first of all sorry for my bad english. The scenario i have is the following:
I am developing a notification services that sends notifications messages to many users. I have the following 3 tables on MySql
users(user_id)
notifications(notification_id, notification)
notifications_log(notification_log_id, notification_id, user_id)
Every time that a user read a notification, i insert a record on notifications_log table, ex. John user with user_id = 2
read the notification with notification_id =3
: "This is a notification", and then i insert a record on notifications_lo
g with user_id = 2
and notification_id = 3
.
There is all ok, but i have to create a query to get all the notifications for all the users that not are inserted on notifications_log. What i have is:
SELECT u.user_id, n.notification_id, n.notification, nl.notification_log_id
FROM users as u
LEFT JOIN notifications_log as nl ON nl.user_id = u.user_id
CROSS JOIN notifications as n
WHERE u.user_id NOT IN (SELECT nl.user_id FROM notifications_log as nl)
AND u.user_id = 1 /* test with user 1 */
If there is no records on notifications_log table of user 1, query results show me
user_id | notification | notification_id | notification_log_id
------------------------------------------------------------------------------
- 1 | Notification_1 | 1 | null
- 1 | Notification_2 | 2 | null
But if i insert at least 1 record on notifications_log
for user and notification_2, then i get empty results, and i should get:
user_id | notification | notification_id | notification_log_id
----------------------------------------------------------------------------
- 1 | Notification_1 | 1 | null
It seems that the query joins the notification_log_id to the other record with null notification_log_id...
In short, what I need it is get all the notifications from a especific user that there are not on inserted on the table notifications_log
Thanks in advance!