我在mysql,varchar列中,日期以d / m / Y格式存储,以为旧输入只有m / Y.
我有查询选择最早的记录:
SELECT expire
FROM logs
WHERE log_id = :logid AND STR_TO_DATE(expire, '%d/%m/%Y') > NOW()
ORDER BY STR_TO_DATE(expire, '%d/%m/%Y') ASC LIMIT 0,1"
如果没有' d' (作为m / Y)在数据库中,如何在没有d的情况下返回行?
答案 0 :(得分:2)
您可以检查str的长度
SELECT expire
FROM logs
WHERE log_id = :logid
AND CASE length(expire)
WHEN 10 THEN STR_TO_DATE(expire, '%d/%m/%Y') > NOW()
WHEN 7 THEN STR_TO_DATE(expire, '%m/%Y) > NOW()
END
ORDER BY STR_TO_DATE(expire, '%d/%m/%Y') ASC LIMIT 0,1;