doulingna9420 2017-06-05 16:08
浏览 43
已采纳

显示读取而不是readed通知

I'm developing a custom notifications system with PHP and MariaDB. I'm struggling with the "read notification" logic.

This is my notifications table:

|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 ...

And this is my notifications_viewed table:

| id | user_id | notification_uid | timestamp  |
+-----------------------------------------------+
| 2  | 1       | xrgoyyzm27       | 1234567890 |

So, what I need is retrieve from the database the notifications and check if that notification has been viewed/readed by the current user.

I'm already showing the notifications (that works just fine) but I don't know how to check if each of the last 5 notifications that I show in the topbar dropdown has been seen/view/read by the current user. Maybe some kinf of SQL join?

  • 写回答

1条回答 默认 最新

  • duanhui9840 2017-06-05 16:12
    关注

    Use 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
    

    BTW, foreign keys usually refer to the primary key of the parent table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备