问题描述
- mysql数据库查询的格式化
-
本人按一个日期段查询表的记录,为保证统一的处理方式,想让查询结果自动补全,如下所示:
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
解决方案二:
不知道应用程序是什么语言,不过肯定可以操作字符串的吧,你把结果再用字符串处理函数处理一下咯