So basically I have two tables:
u_contracts
+--------+---------+------------------------------+
| id | user_id | reschedule_wait_confirm_till |
+--------+---------+------------------------------+
| 107742 | 19982 | 2012-08-08 17:10:41 |
| 38548 | 4622 | 2012-08-08 10:24:42 |
| 98483 | 36707 | 2012-08-08 21:39:08 |
| 45165 | 17632 | 2012-08-08 23:13:57 |
| 131285 | 47527 | 2012-08-08 08:33:58 |
| 131686 | 40194 | 2012-08-08 12:18:09 |
| 91749 | 11021 | 2012-08-08 12:22:05 |
+--------+---------+------------------------------+
restruct_mail_history
+----+---------+---------------------+
| id | user_id | _date |
+----+---------+---------------------+
| 1 | 47527 | 2012-08-07 12:16:51 |
+----+---------+---------------------+
现在,我需要从u_contracts
中选择明天有reschedule_wait_confirm_till
日期的所有user_id,并且restruct_mail_history
中没有今天的用户。
到目前为止,我有一个像这样的工作查询:
SELECT
uc.user_id
FROM
u_contracts uc
WHERE
uc._status = 6
AND DATE( uc.reschedule_wait_confirm_till ) = DATE( ADDDATE( now(), 1 ) )
AND uc.user_id NOT IN ( SELECT rmh.user_id FROM restruct_mail_history rmh WHERE DATE( rmh._date ) = DATE( now() ));
输出:
19982
4622
36707
17632
40194
11021
现在我不喜欢WHERE中的SELECT语句,所以我计划像这样进行LEFT JOIN:
SELECT
uc.user_id
FROM
u_contracts uc
LEFT JOIN
restruct_mail_history rmh
ON
uc.user_id = rmh.user_id
WHERE
uc._status = 6
AND DATE( uc.reschedule_wait_confirm_till ) = DATE( ADDDATE( now(), 1 ) );
输出:
19982
4622
36707
17632
47527
40194
11021
现在这给了我所有的u_contracts
结果,但是当我添加必要的参数AND DATE( rmh._date ) != DATE( now() )
时,它给了我绝对的结果。
知道我做错了吗?
编辑如果iI将最后一行更改为AND DATE( rmh._date ) = DATE( now() )
,它只会向我提供不应该在列表中的结果,这是正确的。为什么它不相反?
谢谢你。
答案 0 :(得分:3)
试试这个:
SELECT uc.user_id
FROM u_contracts uc
LEFT JOIN restruct_mail_history rmh
ON uc.user_id = rmh.user_id
WHERE uc._status = 6 AND
DATE( uc.reschedule_wait_confirm_till ) = DATE( ADDDATE( now(), 1) ) AND
(rmh.user_id IS NULL OR DATE( rmh._date ) != DATE( now() ));
答案 1 :(得分:0)
试试这个
SELECT
uc.user_id
FROM
u_contracts uc
LEFT JOIN
restruct_mail_history rmh
ON
uc.user_id = rmh.user_id and uc._status = 6
AND DATE( uc.reschedule_wait_confirm_till ) = DATE( ADDDATE( now(), -147 ) );