我在我的应用程序中使用了一个消息系统,我希望在他的配置文件中获取未读消息的数量,模式如下
Users table message
user_id message_id
...... sender_id
receiver id
text
flag
标记用于确定用户是否已阅读消息的位置。 flag = 0用户未读取消息,1为读取
如何获取特定用户的未读消息数
SELECT message_id, count(*)
FROM message
WHERE flag=0
GROUP BY receiver_id;
答案 0 :(得分:3)
你快到了,试试这个:
SELECT receiver_id, count(*) as unread_cnt
FROM message
WHERE flag = 0
AND receiver_id = '$particular_user_id' // If you want all users, you can comment out this line.
GROUP BY receiver_id;
您也可以通过以下方式获得读数:
SELECT
receiver_id,
COUNT(CASE WHEN flag = 0 THEN receiver_id END) as unread_cnt,
COUNT(CASE WHEN flag = 1 THEN receiver_id END) as read_cnt
FROM message
WHERE receiver_id = '$particular_user_id' // If you want all users, you can comment out this line.
GROUP BY receiver_id;
答案 1 :(得分:0)
试试这个:
SELECT count(*) as count, message_id
FROM message
WHERE flag=0
GROUP BY receiver_id;