消息系统查询无法正常工作

时间:2012-09-30 12:55:53

标签: mysql sql

我正在尝试构建一个内部邮件系统,用户可以向一个或多个用户发送/回复邮件。我想要像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');

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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