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

Mub*_*her 1 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 |
+--+----------+--------------------+
Run Code Online (Sandbox Code Playgroud)

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

我真的很感激,如果有任何身体帮我做这个查询谢谢

Kay*_*son 5

试一试

SELECT * 
FROM appointments app 
where app.patient_id = 123 AND appointment_start_dt > Now()
order by appointment_start_dt ASC LIMIT 1;
Run Code Online (Sandbox Code Playgroud)