1
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明天日期并且在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() )时,它绝对没有结果。

知道我做错了什么吗?

编辑如果我将最后一行更改为AND DATE( rmh._date ) = DATE( now() ),它只给我应该在列表中的结果,女巫 id 正确。为什么它不能以相反的方式工作?

已经谢谢你了。

4

2 回答 2

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() ));

请参见此处:联接的视觉解释。

于 2012-08-07T09:32:44.597 回答
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 ) ); 
于 2012-08-07T09:34:07.967 回答