我正在製作通知表。也就是說結構是這樣的:閱讀通知表
notifications:
+----+------+----------------------+-----------------------------------+-----------+
| ID | user | notification | message | timestamp |
+----+------+----------------------+-----------------------------------+-----------+
| 1 | 6 | Denied acceptance | You have been denied for... | |
| 2 | 6 | Apreooved acceptance | You have been accepted... | |
| 3 | 0 | Open slots | there are still open slots for... | |
+----+------+----------------------+-----------------------------------+-----------+
和表示該通知是由每一個用戶
notifications_read
+----+------+--------------+-----------+
| ID | user | notification | timestamp |
+----+------+--------------+-----------+
| 1 | 6 | 2 | |
+----+------+--------------+-----------+
讀取如果通知只是針對一個用戶表,有一個用戶ID下的「用戶」在「通知」中 - 如果通知是針對所有人的,我在用戶下插入'0'。
我想做一個查詢,選擇「通知」,即不讀取的行。
我當前的查詢是不正常:
SELECT *
FROM notifications
WHERE id NOT IN
(SELECT notification
FROM notification_read
WHERE user = $id) AND user = $id OR user = 0
因此,對於上面的表格需要與ID 1 & 3,選擇行
感謝您的幫助!
什麼是「不讀」意味着當一個通知是爲所有用戶?您是否只爲特定用戶尋找未讀通知? –