简体   繁体   中英

Stored procedure and select query return different result in MySQL

I am using MySQL and MySQL workbench.

This is my stored procedure:

CREATE DEFINER=`root`@`%` PROCEDURE `sms_stats`(
                IN  fromDate VARCHAR(8), 
                IN  toDate VARCHAR(8), 
                IN  smsCost VARCHAR(8), 
                OUT bank_id int, 
                OUT bank_name VARCHAR(100), 
                OUT totalSms VARCHAR(20),
                OUT totalCost VARCHAR(20)
)
BEGIN
SELECT c.bank_id as bank_id,
       b.name as bank_name,
       count(c.bank_id) as totalSms, 
       (count(c.bank_id) * smsCost) as totalCost 
FROM calendar c
LEFT JOIN banks b on c.bank_id = b.id
WHERE sms_sent = 1 
AND sms_sent_datetime >= fromDate 
AND sms_sent_datetime <= toDate
GROUP BY bank_id, bank_name;
END

Why stored procedure return (I executed with: call prenotabanca.sms_stats('20220101', '20220420', '0.08', @bank_id, @bank_name, @totalSms, @totalCost);) a different result from

SELECT c.bank_id as bank_id,
       b.name as bank_name,
       count(c.bank_id) as totalSms, 
       (count(c.bank_id) * '0.08') as totalCost 
FROM prenotabanca.calendar c
LEFT JOIN prenotabanca.banks b on c.bank_id = b.id
WHERE sms_sent = 1 
AND sms_sent_datetime >= '20220101'
AND sms_sent_datetime <= '20220420'
GROUP BY bank_id, bank_name;

I solved, the problem was in the group by, I didn't use the alias and the column bank id is contained in several tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM