我正在开展一个展示电视节目时间表的小项目。主要问题是我需要显示4个程序,包括当前程序。问题是我真的不知道结果中的第一个程序是否是最新的,或者可能只是前一个程序仍然存在...
例如:
+----+------------+----------+
| id | date | time |
+----+------------+----------+
| 38 | 2016-11-15 | 11:55:00 |
| 39 | 2016-11-15 | 13:00:00 |
| 40 | 2016-11-15 | 13:30:00 |
| 41 | 2016-11-15 | 14:00:00 |
| 42 | 2016-11-15 | 14:30:00 |
| 43 | 2016-11-15 | 15:00:00 |
| 44 | 2016-11-15 | 15:30:00 |
| 45 | 2016-11-15 | 16:30:00 |
| 46 | 2016-11-15 | 18:30:00 |
| 47 | 2016-11-15 | 19:20:00 |
+----+------------+----------+
所以基本上我需要获得4行,首先应该显示是否存在。
答案 0 :(得分:2)
你还不是很清楚时间段属于一个程序,所以我猜这就是你要找的东西
SELECT * FROM shows where id >=
(SELECT id FROM shows
WHERE `time` <= '13:33' AND `date` = '2016-11-15'
ORDER BY `time` DESC LIMIT 1)
LIMIT 4;
答案 1 :(得分:1)
相当直接:
SELECT *
FROM shows
WHERE TIMESTAMP(date, time) >= (
SELECT TIMESTAMP(date, time)
FROM shows
WHERE TIMESTAMP(date, time) <= CURRENT_TIMESTAMP
ORDER BY TIMESTAMP(date, time) DESC
LIMIT 1
)
ORDER BY TIMESTAMP(date, time)
LIMIT 4
当前时间为2016-11-15 12:59:59
的结果:
+------+------------+----------+
| id | date | time |
+------+------------+----------+
| 38 | 2016-11-15 | 11:55:00 |
| 39 | 2016-11-15 | 13:00:00 |
| 40 | 2016-11-15 | 13:30:00 |
| 41 | 2016-11-15 | 14:00:00 |
+------+------------+----------+
当前时间为2016-11-15 13:00:00
的结果:
+------+------------+----------+
| id | date | time |
+------+------------+----------+
| 39 | 2016-11-15 | 13:00:00 |
| 40 | 2016-11-15 | 13:30:00 |
| 41 | 2016-11-15 | 14:00:00 |
| 42 | 2016-11-15 | 14:30:00 |
+------+------------+----------+
TIMESTAMP
函数用于组合日期和时间。