假如我有個數據表,裡面只有“2007-05-29”的一條數據,
我希望得到的結果是
月份 數據
1 0
2 0
3 0
4 0
5 1
6 0
7 0
8 0
9 0
10 0
11 0
12 0
有個主鍵商品id,銷售日期date,,,mysql的數據庫,有勞各位了~~~
現寫了一個,日期自己再加工加工吧
create table MONTH_TABLE(
MONTH_NUM INT
)
CREATE TABLE TESTDATE(
GOOD_KEY VARCHAR(60),
GOOD_DATE VARCHAR(60)
)
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(1);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(2);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(3);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(4);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(5);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(6);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(7);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(8);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(9);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(10);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(11);
INSERT INTO MONTH_TABLE(MONTH_NUM) VALUES(12);
INSERT INTO TESTDATE(GOOD_KEY,GOOD_DATE) VALUES('ASDFA','2007-05-09');
select MT.MONTH_NUM 日期,ifnull(total_num,0) as 數量 from
MONTH_TABLE MT left join
(
SELECT T.*,MONTH(GOOD_DATE)+0 M FROM (
select count(good_key) TOTAL_NUM,GOOD_DATE from TESTDATE group by GOOD_DATE
) T) t on t.m = MT.MONTH_NUM
order by MT.MONTH_NUM