0

我正在开发一个私人消息系统,它看起来更像是一个社交网络消息传递。没有标题,相关的 pms 显示为论坛主题。

我有两张桌子来跟踪下午

pm_sessions  : pm_session  , sender , receiver, pm_counter 

pm           : id , pm_session  , text , date , sender

pm_session 就像 pm 的包装器一样工作

现在在用户配置文件中,我想显示从当前用户发送或接收 pm 的用户列表

问题是我不想分别显示接收者和发送者用户,我希望将所有与我的人交谈的用户都放在一个列表中

这就是我的想法,我现在它不适用于所有 IF 语句

$current_user = $_session['userid'];
    $query = " 

 select pms.sender , pms.receiver, 
 count(pms.otheruser) as total_dialogs_with_this_user , 
 u.username as other_user_name  , u.id as other_user_id

 from pm_session pms

   //// joining to the users table on the other user id
   if($current_user = pms.sender)
     JOIN users u on pms.receiver= u.id 
   if($current_user = pms.receiver)
     JOIN users u on  pms.sender = u.id 

  WHERE pms.reciver = $current_user || pms.sender = $current_user

   /// grouping by other user id
   if($current_user = pms.sender)
     group by pms.receiver
   if($current_user = pms.receiver)
     group by pms.sender ";

我希望结果是这样的

HELLO max ... you have been talking to :

ross... 3 dialogs
joey ... 4 dialogs
chandler ... 5 dialogs

我可以很容易地用两个查询来写这个,一个用于接收者,一个用于发送者,但就像我说的我不想将它们分开

4

1 回答 1

2
SELECT   CASE $current_user
           WHEN pms.sender   THEN pms.receiver
           WHEN pms.receiver THEN pms.sender
         END                  AS other_user_id,
         u.username           AS other_user_name,
         COUNT(pms.otheruser) AS total_dialogs_with_this_user
FROM     pm_session           AS pms
  JOIN   users                AS u
           ON u.id = CASE $current_user
             WHEN pms.sender   THEN pms.receiver
             WHEN pms.receiver THEN pms.sender
           END
GROUP BY other_user_id, other_user_name
于 2012-05-31T09:01:52.723 回答