简体   繁体   English

如何获取 mysql 数据库中两个日期之间的状态?

[英]How to get status between two dates in mysql database?

I have a query like this to get status without adding fields.我有一个这样的查询来获取状态而不添加字段。

SELECT * ,
      IF(DATEDIFF(STR_TO_DATE(end_date, "%Y-%m-%d"), CURDATE())<=0,"End","Running") status 
FROM agreements

the query is running but I want to add status if the end date is less than 3 days then the status will show "will be ended"查询正在运行,但如果结束日期少于 3 天,我想添加状态,则状态将显示“将结束”

so there will be 3 statuses in the query, END, RUNNING, Will be END所以查询中会有3个状态,END, RUNNING, Will be END

You can use CASE..WHEN statements;您可以使用CASE..WHEN语句; also your STR_TO_DATE(end, '%Y-%m-%d') usage is unnecessary because DATEDIFF() function considers date part only for the calculation:您的STR_TO_DATE(end, '%Y-%m-%d')使用也是不必要的,因为DATEDIFF() function 仅将日期部分考虑用于计算:

SELECT * ,
       CASE WHEN DATEDIFF(end_date, CURDATE()) <= 0 THEN 'End' 
            WHEN DATEDIFF(end_date, CURDATE()) < 3 THEN 'Will Be End' 
            ELSE 'Running'
       END status
FROM agreements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM