0

我有两张通知表和一张朋友列表。一个通知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

现在我想要一个带有我现有查询的查询,以获取朋友未读的通知。

4

2 回答 2

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 评论。

于 2012-08-13T10:27:38.370 回答
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;
于 2012-08-13T10:27:49.103 回答