如果有人可以帮助我,我真的被高级查询阻止了
我有一个表mysql,看起来像:
客户(id,appointment_date1,appointment_date2,appointment_date3,appointment_date4)
我正在寻找一个查询,列出下一个最近的约会是什么
在我执行此查询之前:
SELECT CASE
WHEN (customers.appointment_date1 != '0000-00-00' AND DATE(customers.appointment_date1) >= CURDATE()) THEN customers.appointment_date1
WHEN (customers.appointment_date2 != '0000-00-00' AND DATE(customers.appointment_date2) >= CURDATE()) THEN customers.appointment_date2
WHEN (customers.appointment_date3 != '0000-00-00' AND DATE(customers.appointment_date3) >= CURDATE()) THEN customers.appointment_date3
WHEN (customers.appointment_date4 != '0000-00-00' AND DATE(customers.appointment_date4) >= CURDATE()) THEN customers.appointment_date4
END as appointment
ORDER BY appointment ASC
但它错了,它无法正常工作。
任何人都可以提供帮助吗?
由于
答案 0 :(得分:1)
我在if()
子句中使用嵌套的mysql select
函数,例如:
select *
from(
select if(date1<date2&&date1>curdate(),date1,
if(date2<date3&&date2>curdate(),date2,
if(date3>curdate(),date3, 'nothing')
)
) as date
from dates
) as dates
order by dates.date desc;
编辑:根据Zika的评论
SELECT IF(LEAST(
IFNULL(date1,'0000-00-00'),
IFNULL(date2,'0000-00-00'),
IFNULL(date3,'0000-00-00')
)!='0000-00-00',
LEAST(
IFNULL(date1,'0000-00-00'),
IFNULL(date2,'0000-00-00'),
IFNULL(date3,'0000-00-00')
),
'aucune date'
)
FROM dates;