2

我正在尝试构建一个内部消息传递系统,用户可以在其中向一个或多个用户发送/回复消息。我想要显示收件箱,如 gmail 系统。原始消息将列在表中。如果有与该消息对应的回复,则回复计数显示在侧面。如果消息或回复中的任何一个未读,我希望行以粗体显示。我有以下表结构。

msg_inbox 表

message_id
message_subject
message_body
sender_id
recipient_id
read_status
message_date
marked_important

msg_reply 表

reply_id
message_id
reply_subject
reply_body
reply_sender_id
reply_recipient_id
reply_read_status
reply_date

到目前为止,我有以下查询,但似乎不起作用

SELECT 
  m.*,
  COUNT(r.reply_id) replycount,
  (SELECT 
    reply_read_status 
  FROM
    sc_messages_reply 
  WHERE message_id = r.message_id 
    AND reply_recipient_id = r.reply_recipient_id 
    AND reply_read_status='0'
  ) AS reply_read_status,
  r.reply_sender_id,
  r.reply_date 
FROM
  sc_messages m 
  LEFT OUTER JOIN sc_messages_reply r 
    ON m.message_id = r.message_id 
WHERE m.sender_id = '34' 
  OR m.recipient_id = '34' 
  OR r.reply_sender_id = '34' 
  OR r.reply_recipient_id = '34' 
GROUP BY m.message_id 
ORDER BY m.marked_important DESC,
  m.message_date DESC,
  r.reply_date DESC 

我在上面的查询中一定有什么地方出错了,如果有人指出它会很棒。

这是样本数据

msg_inbox

insert into `msg_inbox`
(`message_id`, `message_subject`, `message_body`, `sender_id`,
 `recipient_id`, `read_status`, `message_date`, `marked_important`) 
 values (1, 'This is test message',
         'This is test message body apart from subject', 
         12, 34, 1, '2012-09-29 07:49:37', 1), 
        (2, 'This is yet another message',
         'This is yet another subject', 13, 34, 1, '2012-09-29 07:51:00', 0);

msg_reply

insert into `msg_reply`
(`reply_id`, `message_id`, `reply_subject`, `reply_body`, `reply_sender_id`,
 `reply_recipient_id`, `reply_read_status`, `reply_date`)
 values (1, 2, 'Test reply', 'test body\r\n', 
         34, 13, 1, '2012-09-29 07:51:46'),          
        (2, 2, 'Reply of the reply\r\n', 'Message reply of the reply',
         13, 34, 1, '2012-09-29 07:52:32'),
        (4, 1, 'Re.This is test message', 
         'Naturally I took it upon myself to solve this problem for all of you :) I created this plugin to be as general purpose as possible. As a result it ended up being vastly more customizable than any other jQuery auto-complete plugin. Not only that, I am using actual jQuery. For some reason all the other "jQuery" plugins don't really take advantage of the radness that is jQuery. Consequently, my plugin ended up being',34,12,1,'2012-09-30 14:37:40'),(5,1,'Re.This is test message','Well its all good sir',12,34,0,'2012-09-30 15:07:38');
4

2 回答 2

2

我想我已经解决了我将子查询更改为的问题

SELECT 
    reply_read_status 
  FROM
    sc_messages_reply 
  WHERE message_id = r.message_id 
    AND  reply_read_status='0' AND reply_recipient_id='34'
于 2012-09-30T13:37:57.093 回答
1

我想你必须JOIN上桌

SELECT 
  m.*,
  COUNT(r.reply_id) replycount, unreaded.reply_read_status,
  r.reply_sender_id,
  r.reply_date 
FROM sc_messages m 
INNER JOIN
(
     SELECT *
     FROM sc_messages_reply  
     WHERE reply_read_status = '0'
) unreaded ON m.message_id = unreaded.message_id
INNER JOIN  -- You can also join the readed messages like this:
(
     SELECT *
     FROM sc_messages_reply  
     WHERE reply_read_status = '1'
) readed ----
LEFT OUTER JOIN sc_messages_reply r ON m.message_id = r.message_id 
  AND unreaded.reply_recipient_id = r.reply_recipient_id
WHERE m.sender_id = '34' 
  OR m.recipient_id = '34' 
  OR r.reply_sender_id = '34' 
  OR r.reply_recipient_id = '34' 
GROUP BY m.message_id 
ORDER BY m.marked_important DESC,
  m.message_date DESC,
  r.reply_date DESC 
于 2012-09-30T13:59:34.090 回答