我有一张可用日期表,我想从中制作一张不可用日期表。看起来应该很简单,可以在php中做到没有问题,但无法弄清楚如何在MySQL查询中做到这一点。
来源表: (打开是某人签出的日期;关闭是某人签到的日子)
+------------+------------+
| opens | closes |
+------------+------------+
| 2015-12-03 | 2015-12-09 |
| 2016-01-03 | 2016-01-06 |
| 2016-01-08 | 2016-01-15 |
| 2016-01-16 | 2016-02-08 |
| 2016-02-21 | 2016-02-27 |
| 2016-03-01 | 2016-03-06 |
| 2016-03-10 | 2016-12-03 |
+------------+------------+
查询应返回:
+------------+------------+
| checkin | checkout |
+------------+------------+
| 2015-12-09 | 2016-01-03 |
| 2016-01-06 | 2016-01-08 |
| 2016-01-15 | 2016-01-16 |
| 2016-02-08 | 2016-02-21 |
| 2016-02-27 | 2016-03-01 |
| 2016-03-06 | 2016-03-10 |
+------------+------------+
感谢您的帮助!
答案 0 :(得分:1)
尝试此查询:
SELECT Closes as checkin,
(SELECT OPENS FROM T WHERE OPENS>T1.Closes ORDER BY OPENS LIMIT 1) as checkout
FROM T T1
WHERE EXISTS (SELECT OPENS FROM T WHERE OPENS>T1.Closes)