所以這是我的查詢
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
我做了一些谷歌搜索,但仍然沒有運氣。
uj5u.com熱心網友回復:
您在程序引數中使用了 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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/491392.html
標籤:mysql
上一篇:Postgres按行分組查詢結果
