混淆查询添加日期不在表的mysql中

时间:2013-09-26 09:48:40

标签: mysql sql date

不好意思, 我将添加日期,如果该日期不在mysql表中 例如我有桌子

ID      |   name    |   date        |   clock in    |   clock out
---------------------------------------------------------------
1001    |   A       |   2013-09-18  |   09:40       |   17:15
1001    |   A       |   2013-09-20  |   08:20       |   17:35
1001    |   A       |   2013-09-21  |   08:40       |   17:40
1001    |   A       |   2013-09-23  |   08:10       |   17:50

所以,如何将日期添加到无表并设置时钟和时钟输出设置为null 示例

ID      |   name    |   date        |   clock in    |   clock out
---------------------------------------------------------------
1001    |   A       |   2013-09-18  |   09:40       |   17:15
1001    |   A       |   2013-09-19  |   null        |   null
1001    |   A       |   2013-09-20  |   08:40       |   17:40
1001    |   A       |   2013-09-21  |   08:10       |   17:50
1001    |   A       |   2013-09-22  |   null        |   null
1001    |   A       |   2013-09-23  |   08:10       |   17:50

感谢

1 个答案:

答案 0 :(得分:0)

如果您仍然感兴趣,这就是您所需要的:

SELECT 
COALESCE(id,1001) id,
COALESCE(name,'A') name,
fixed_days.fixed_date,
clockin,clockout
FROM t 
RIGHT JOIN 
(SELECT CURDATE() as fixed_date 
UNION ALL SELECT CURDATE() - INTERVAL 1 day 
UNION ALL SELECT CURDATE() - INTERVAL 2 day 
UNION ALL SELECT CURDATE() - INTERVAL 3 day 
UNION ALL SELECT CURDATE() - INTERVAL 4 day 
UNION ALL SELECT CURDATE() - INTERVAL 5 day 
UNION ALL SELECT CURDATE() - INTERVAL 6 day) AS fixed_days 
ON t.record_date = fixed_days.fixed_date
WHERE fixed_days.fixed_date <= (select max(record_date) from t);

<强> SQL Fiddle..