我正在使用 PHP 和 MariaDB 开发自定义通知系统。我正在努力解决“阅读通知”逻辑。
这是我的通知
表:
|id | title | content | uid | visibility | timestamp |
+------------------------------------------------------------------------+
|11 | Other title | Other content | sz46j86rd | public | 0123456789 |
|10 | My title | My content | xrgoyyzm27 | private | 0123456789 |
... And so on ...
这是我的 notifications_viewed
表:
| id | user_id | notification_uid | timestamp |
+-----------------------------------------------+
| 2 | 1 | xrgoyyzm27 | 1234567890 |
因此,我需要从数据库中检索通知并检查当前用户是否已查看/读取该通知。
我已经显示了通知(效果很好),但我不知道如何检查当前用户是否已看到/查看/阅读了我在顶栏下拉列表中显示的最后 5 个通知。也许是 SQL join
的某种形式?
最佳答案
使用LEFT JOIN
SELECT n.*, IF(nv.id IS NULL, 'No', 'Yes') AS viewed
FROM notifications AS n
LEFT JOIN notifications_viewed AS nv ON n.uid = nv.notification_uid AND nv.user_id = :current_user
顺便说一句,外键通常指的是父表的主键。
关于php - 显示已读和未读通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44373086/