本人按一個日期段查詢表的記錄,為保證統一的處理方式,想讓查詢結果自動補全,如下所示:
billRec
100元,5人,2015-09-15
200元,6人,2015-09-21
而我想在查詢日期為2015-09-15至2015-09-30這個日期區間時顯示如下結果:
100元,5人,2015-09-15
0,0,2015-09-16
0,0,2015-09-17
……
200元,6人,2015-09-21
0,0,2015-09-22
0,0,2015-09-23
……
0,0,2015-09-30
也就是不存在的日期自動補為0,日期字段要顯示全部。
請高手給個方法,最好是sql實現,如果根本就不應該這麼處理,那如何對返回的記錄集DataTable上作處理,頓首百拜啊
DROP TEMPORARY TABLE IF EXISTS TABLE_1;
CREATE TEMPORARY TABLE TABLE_1
(
SELECT *
FROM
(
SELECT '100YUAN' vMoney,'5REN' vNum,'2015-09-15' vDate
UNION ALL
SELECT '200YUAN','6REN','2015-09-21'
)a
);
DROP TEMPORARY TABLE IF EXISTS TABLE_2;
CREATE TEMPORARY TABLE TABLE_2
(
SELECT *
FROM
(
SELECT '2015-09-15' vDate
UNION ALL
SELECT '2015-09-16'
UNION ALL
SELECT '2015-09-17'
UNION ALL
SELECT '2015-09-18'
UNION ALL
SELECT '2015-09-19'
UNION ALL
SELECT '2015-09-20'
UNION ALL
SELECT '2015-09-21'
UNION ALL
SELECT '2015-09-22'
UNION ALL
SELECT '2015-09-23'
UNION ALL
SELECT '2015-09-24'
) a
);
SELECT a.vDate,IFNULL(b.vMoney,0) vMoney,IFNULL(b.vNum,0) vNum
FROM TABLE_2 a
LEFT JOIN TABLE_1 b ON a.vDate = b.vDate
ORDER BY a.vDate