Mysql Query获取日期时间紧跟在当前日期时间之后的记录

时间:2014-01-16 14:33:57

标签: mysql sql database

我有一个包含以下字段的约会表。我写了这样的查询。

SELECT * 
FROM appointments app 
WHERE app.patient_id = 123
ORDER BY appointment_start_dt DESC;
+--+----------+--------------------+
|id|patient_id|appointment_start_dt|
+--+----------+--------------------+
|1 |123       |2014-01-18 19:10:00 |
+--+----------+--------------------+
|2 |123       |2014-01-18 12:08:00 |
+--+----------+--------------------+
|3 |123       |2014-01-17 15:00:00 |
+--+----------+--------------------+
|4 |123       |2014-01-15 11:01:00 |
+--+----------+--------------------+
|5 |123       |2014-01-11 12:30:00 |
+--+----------+--------------------+
|6 |123       |2014-01-10 04:00:01 |
+--+----------+--------------------+

表格在当前日期之前有一些约会(假设当前日期时间是2014-01-15 15:00:00),并且在特定患者的当前日期和时间之后有一些约会。 我需要针对一位患者取一条记录,该患者的预约日期是在当前日期时间之后立即到来的,即如果在2014-01-17 15:00:00之后有两次预约,我需要选择预约最早的是2014-01-18 12:08:00。

我真的很感激,如果有人帮我做这个查询 感谢

1 个答案:

答案 0 :(得分:5)

试一试

SELECT * 
FROM appointments app 
where app.patient_id = 123 AND appointment_start_dt > Now()
order by appointment_start_dt ASC LIMIT 1;
相关问题