-4

对不起,如果该日期不在 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

那么,如何将日期添加到无表并设置时钟输入和时钟输出设置空示例

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

谢谢

4

1 回答 1

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小提琴..

于 2013-09-26T11:43:44.487 回答