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 |
+----+---------+---------------------+
Now I need to select all user_id’s from u_contracts that have date reschedule_wait_confirm_till for tomorrow and that have no todays entires in restruct_mail_history.
So far I have a working query like this:
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() ));
Output:
19982
4622
36707
17632
40194
11021
Now I don’t like the SELECT statement in WHERE, so I was planing to make LEFT JOIN like this:
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 ) );
Output:
19982
4622
36707
17632
47527
40194
11021
Now this gives me all u_contracts result, but when I add the necessary parameter AND DATE( rmh._date ) != DATE( now() ) it gives me absolutelu no results.
Any idea what I’m doing wrong?
EDIT If iI change the last line to AND DATE( rmh._date ) = DATE( now() ), it gives me only the results that should not be in the list, witch id correct. Why doesn’t it work the opposite way?
Thank you already.
try this:
see here: Visual Explanation Of Joins.