[英]Different result between Query and stored procedure
所以這是我的查詢
SELECT count(distinct (id_customer)) as customer
from `orders`
WHERE order_date >= '2022-06-01 08:30%' AND order_date <= '2022-06-06 08:30%';
結果是2069 。
然后我創建這個存儲過程
CREATE PROCEDURE customer_count(IN start_date DATE, IN end_date DATE)
BEGIN
SELECT count(distinct (id_customer)) as customer
from `orders`
WHERE order_date >= start_date AND order_date <= end_date;
END;
然后使用
CALL customer_count('2022-06-01 08:30%', '2022-06-06 08:30%');
它返回2126
我做了一些谷歌搜索,但仍然沒有運氣。
您在過程參數中使用了 DATE 數據類型,因此它只比較日期值。
請將 DATE 數據類型更改為 DATETIME,您將獲得預期的輸出,並且不要在日期列中使用 %(通配符搜索)。
更好地使用以下查詢:
SELECT count(distinct (id_customer)) as customer
from `orders`
WHERE order_date >= '2022-06-01 08:30:00' AND order_date <= '2022-06-06 08:30:59';
更改程序如下:
CREATE PROCEDURE customer_count(IN start_date DATETIME, IN end_date DATETIME)
BEGIN
SELECT count(distinct (id_customer)) as customer
from `orders`
WHERE order_date >= start_date AND order_date <= end_date;
END;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.