如果没有更新的记录,请选择MySQL
我正试图从MySQL中提取即将到期的合同。我做了以下陈述:如果没有更新的记录,请选择MySQL,mysql,select,where,Mysql,Select,Where,我正试图从MySQL中提取即将到期的合同。我做了以下陈述: SELECT c.id, c.file, c.user, c.contractno, c.startdate, c.enddate, f.name, f.path FROM signedcontracts c LEFT JOIN files f ON c.file = f.id WHERE c.enddate < NOW() + INTERVAL 30 DAY AND c.enddate >
SELECT
c.id, c.file, c.user, c.contractno, c.startdate, c.enddate, f.name, f.path
FROM signedcontracts c
LEFT JOIN files f ON c.file = f.id
WHERE c.enddate < NOW() + INTERVAL 30 DAY
AND c.enddate > NOW()
ORDER BY c.enddate ASC
但我不知道如何确保它不会为已签署合同中已有新合同的用户返回任何合同。也许您只需要检查一下,现在+30天后是否没有结束日期
MariaDB [sandbox]> select * from t;
+------+------------+------------+
| id | start_date | end_date |
+------+------------+------------+
| 1 | 2017-10-01 | 2017-10-31 |
| 1 | 2017-12-01 | 2017-12-31 |
| 2 | 2017-10-01 | 2017-12-31 |
| 2 | 2018-10-01 | 2018-10-31 |
| 3 | 2018-10-01 | 2018-10-31 |
| 4 | 2017-10-01 | 2018-10-31 |
+------+------------+------------+
6 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]>
MariaDB [sandbox]> select *
-> from t
-> where end_date between now() and now() + interval 30 day
-> and id not in (select id from t where end_date > now() + interval 30 day);
+------+------------+------------+
| id | start_date | end_date |
+------+------------+------------+
| 1 | 2017-12-01 | 2017-12-31 |
+------+------------+------------+
1 row in set (0.00 sec)
我用这个查询得到了它:
SELECT
c.id, c.file, c.user, c.contractno, c.startdate, c.enddate, f.name, f.path
FROM signedcontracts c
LEFT JOIN files f ON c.file = f.id
WHERE c.enddate < NOW() + INTERVAL 30 DAY
AND c.enddate > NOW()
AND c.user NOT IN (SELECT user FROM signedcontracts WHERE enddate > NOW() + INTERVAL 30 DAY)
ORDER BY c.enddate ASC
检查用户是否与截止日期前30天以上的合同签订如果可能,请在问题中添加一些数据。您想要什么数据?请阅读并执行。PS:已经签订新合同意味着什么?如果不希望表示所有的左表行,为什么要左加入?