使用GROUP BY,ORDER BY,GROUP_CONCAT获取最旧的行

时间:2015-03-24 16:23:26

标签: mysql sql

我正在尝试使用以下语句来获取已登录用户的消息列表。问题是使用组和订单进行调度,因为它并不总是显示最新的消息。任何人都可以对此有所了解吗?我应该使用子查询吗?

SELECT 
  messages.message_id,
  messages.message_parent_id,
  messages.message_project_id,
  messages.message_from_id,
  messages.message_from_email,
  messages.message_subject,
  messages.message_body,
  messages.message_sent_datetime,
  CONCAT(
    u_from.forename,
    ' ',
    u_from.surname
  ) AS from_x,
  GROUP_CONCAT(
    CONCAT(
      u_to.user_id,
      '||',
      u_to.forename,
      ' ',
      u_to.surname
    )
  ) AS to_x,
  m_recipients.recipient_message_read 
FROM
  messages 
  INNER JOIN users AS u_from 
    ON messages.message_from_id = u_from.user_id 
  INNER JOIN message_recipients AS m_recipients 
    ON messages.message_id = m_recipients.recipient_message_id 
  INNER JOIN users AS u_to 
    ON m_recipients.recipient_user_id = u_to.user_id 
WHERE (
    m_recipients.recipient_user_id = '$current_user_id' 
    OR messages.message_from_id = '$current_user_id'
  ) 
  AND messages.message_project_id = '$project_id' 
GROUP BY messages.message_parent_id 
ORDER BY messages.message_sent_datetime DESC 

小提琴:http://sqlfiddle.com/#!9/7fbfa/3

2 个答案:

答案 0 :(得分:0)

你确定你不需要吗?

GROUP BY messages.message_parent_id, messages.message_id

如果您确定将messages.message_sent_datetime替换为

MAX(messages.message_sent_datetime) as message_sent_datetime,
...
ORDER BY message_sent_datetime

MIN(messages.message_sent_datetime) as message_sent_datetime,
...
ORDER BY message_sent_datetime

好的尝试this way

SELECT 
  messages.message_id,
  messages.message_parent_id,
  messages.message_project_id,
  messages.message_from_id,
  messages.message_from_email,
  messages.message_subject,
  messages.message_body,
  MAX(messages.message_sent_datetime) AS message_sent_datetime,
  messages.message_important,
  u_from.from_x,
  GROUP_CONCAT(u_to.to_x),
  m_recipients.recipient_message_read 
FROM
  messages 
  INNER JOIN (
    SELECT 
      user_id,
      CONCAT(forename,' ',surname) AS from_x
    FROM users 
    ) AS u_from 
    ON messages.message_from_id = u_from.user_id 
  INNER JOIN message_recipients AS m_recipients 
    ON messages.message_id = m_recipients.recipient_message_id 
  INNER JOIN 
     (SELECT
        user_id,
        CONCAT(user_id,'||',forename,' ',surname) AS to_x
      FROM users 
     ) AS u_to 
    ON m_recipients.recipient_user_id = u_to.user_id 
WHERE (
    m_recipients.recipient_user_id = 1
    OR messages.message_from_id = 1
  ) 
  AND messages.message_project_id = 1
GROUP BY messages.message_parent_id
ORDER BY messages.message_sent_datetime DESC

答案 1 :(得分:0)

所以最终我想出来了

SELECT

messages.message_id,
messages.message_parent_id,
messages.message_project_id,
messages.message_from_id,
messages.message_from_email,
messages.message_subject,
messages.message_body,
messages.message_sent_datetime, 
messages.message_important,

CONCAT(u_from.forename, ' ', u_from.surname) as from_x,
GROUP_CONCAT(CONCAT(u_to.forename, ' ', u_to.surname)) as to_x, 

m_recipients.recipient_message_read

FROM (select * from messages WHERE message_project_id = 1 ORDER BY message_sent_datetime desc) messages

INNER JOIN users AS u_from 
    ON messages.message_from_id = u_from.user_id 
INNER JOIN message_recipients AS m_recipients 
    ON messages.message_id = m_recipients.recipient_message_id 
INNER JOIN users AS u_to 
    ON m_recipients.recipient_user_id = u_to.user_id  

WHERE ( m_recipients.recipient_user_id = 1 OR messages.message_from_id = 1 ) 

GROUP BY message_parent_id

http://sqlfiddle.com/#!9/12a2d/3