我想在mysql中填写今天和30天前的日期。
例如:
Date Value
2015-08-05 1
2015-08-04 2
2015-08-03 0
......
2015-07-05 1
下面是mysql:
SELECT IFNULL(SUM(units.price), 0) as price, DATE(units.solddate) as date, DAY(units.solddate) as day,
(
select a.Date
from (
select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date >= DATE_SUB(NOW(), INTERVAL 30 day)
) as onemonth
FROM (tables)
GROUP BY date
但是,我收到了这个错误:#1242 - 子查询返回超过1行我理解这个错误。
请指教。谢谢。
答案 0 :(得分:1)
在更好地了解您要实现的目标后,我将这个MySQL
查询放在一起。
我测试了它,它对我有用。
SELECT
`dates_table`.`date`
-- ,`dates_table`.`day`
,SUM( COALESCE( `units`.`price` ,0 ) ) AS `price`
FROM
-- Start of query for making dates_table
(
SELECT
DATE( DATE_SUB( CURDATE( ) ,INTERVAL `intervals_table`.`days` DAY ) ) AS `date`
,DAY( DATE_SUB( CURDATE( ) ,INTERVAL `intervals_table`.`days` DAY ) ) AS `day`
FROM
(
SELECT 0 AS `days`
UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8
UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12
UNION SELECT 13 UNION SELECT 14 UNION SELECT 15 UNION SELECT 16
UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION SELECT 20
UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24
UNION SELECT 25 UNION SELECT 26 UNION SELECT 27 UNION SELECT 28
UNION SELECT 29 UNION SELECT 30 UNION SELECT 31
) `intervals_table`
HAVING
`date` >= DATE_SUB( CURDATE( ) ,INTERVAL 30 DAY )
)
-- End of query for making dates_table
`dates_table`
LEFT OUTER JOIN `units` ON
`units`.`sold_date` >= CONCAT( `dates_table`.`date` ,' 00:00:00' )
AND `units`.`sold_date` <= CONCAT( `dates_table`.`date` ,' 23:59:59' )
WHERE
1
GROUP BY
`dates_table`.`date`
ORDER BY
`dates_table`.`date`
我用于测试的units
表是:
CREATE TABLE IF NOT EXISTS `units` (
`id` int(11) NOT NULL AUTO_INCREMENT
,`price` int(11) NOT NULL
,`sold_date` datetime NOT NULL
,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `units` (`id`, `price`, `sold_date`) VALUES
(1, 10, '2015-08-02 00:00:00'),
(2, 11, '2015-08-03 00:00:00'),
(3, 14, '2015-08-03 00:00:00'),
(4, 3, '2015-08-04 00:00:00');
结果
date price
---------- -----
2015-07-07 0
2015-07-08 0
2015-07-09 0
2015-07-10 0
2015-07-11 0
2015-07-12 0
2015-07-13 0
2015-07-14 0
2015-07-15 0
2015-07-16 0
2015-07-17 0
2015-07-18 0
2015-07-19 0
2015-07-20 0
2015-07-21 0
2015-07-22 0
2015-07-23 0
2015-07-24 0
2015-07-25 0
2015-07-26 0
2015-07-27 0
2015-07-28 0
2015-07-29 0
2015-07-30 0
2015-07-31 0
2015-08-01 0
2015-08-02 10
2015-08-03 25
2015-08-04 3
2015-08-05 0
答案 1 :(得分:1)
通过纠正您的方法,这是一个解决方案
SELECT
`dates`.`date` ,SUM( COALESCE( `units`.`price` ,0 ) ) AS `price`
FROM
(
-- Start of query for making dates
SELECT
`dates`.`date`
,CONCAT( `dates`.`date` ,' 00:00:00' ) AS `day_start_datetime`
,CONCAT( `dates`.`date` ,' 23:59:59' ) AS `day_end_datetime`
FROM
(
SELECT
DATE_SUB( CURDATE( ) ,INTERVAL `intervals`.`days` DAY ) AS `date`
FROM
(
SELECT
( `hundreds_place`.`num` * 100 )
+( `tens_place`.`num` * 10 )
+( `ones_place`.`num` * 1 ) AS `days`
FROM ( SELECT 0 AS `num` UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) AS `ones_place`
JOIN ( SELECT 0 AS `num` UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) AS `tens_place`
JOIN ( SELECT 0 AS `num` UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) AS `hundreds_place`
) `intervals`
HAVING
`date` >= DATE_SUB( CURDATE() ,INTERVAL 30 DAY )
) `dates`
-- End of query for making dates
) `dates`
LEFT OUTER JOIN `units`
ON `units`.`sold_date` BETWEEN `day_start_datetime` AND `day_end_datetime`
WHERE
1
GROUP BY
`dates`.`date`
ORDER BY
`dates`.`date`