我有两个表用于通知,一个表用于friend_list。一个通知tbale具有创建和键入的通知信息以及标记哪个朋友已阅读通知的全部和第二。能够检索用户创建的通知,仅显示给用户的朋友。但是现在我想从名为通知读取的第二个表中显示未读给用户朋友的通知。
我所做的查询是(来自notification + friend_list表)
$sql3=mysql_query("select n.*,f.friend_id,f.uid,f.status from notification n,friend_list f where f.uid='$id' and f.status='1' and n.title_text='Global Notification' and n.user_id in ('$id' , f.friend_id) and n.owner_user_id=f.friend_id order by n.time_stamp desc");
现在如何获取朋友未读的通知?
第二个表是
uid (friend_id)
notification_id
is_read
现在我希望使用现有查询进行查询,以获取朋友未读的通知。
答案 0 :(得分:0)
让我们说:
'$id'
是提出通知的用户的ID n.user_id
是提出通知的用户n.owner_user_id
是接收通知的用户查询是:
select
n.*,f.friend_id,f.uid,f.status
from
notification n
inner join
friend_list f
on n.owner_user_id=f.friend_id
inner join
notification_read nr
on nr.notification_id = n.notification_id and
nr.uid = f.friend_id
where
f.status='1' and
n.title_text='Global Notification' and
n.user_id = '$id' and
nr.is_read = 0
order by n.time_stamp desc
已编辑 由于新的OP评论。
答案 1 :(得分:0)
试试这个:
SELECT n.*,f.friend_id,f.uid,f.status
FROM notification n
INNER JOIN friend_list f
ON n.owner_user_id = f.friend_id
INNER JOIN second_table s
ON f.friend_id = s.uid AND
n.notification_id = s.notification_id
WHERE f.uid='$id' AND
f.status='1' AND
n.title_text='Global Notification' AND
n.user_id IN ('$id' , f.friend_id) AND
s.is_read = 0
ORDER BY n.time_stamp DESC;