简体   繁体   English

MySql查询,用于从对话和消息表联接中选择最新消息

[英]MySql query for selecting latest message from the conversation and messages table join

Please tell me what is wrong with this code, this returns all the conversations, but it returns latest message for only first conversation. 请告诉我这段代码有什么问题,它返回所有对话,但仅返回第一次对话的最新消息。

    SELECT 
        conversations.*,
        messages.message,
        patients.first_name as fullname,
        patients.city,
        patients.thumb,
        patients.gender,
        patients.online_status
    FROM 
        patients
    INNER JOIN conversations
        ON conversations.patient_id_fk = patients.id
    LEFT JOIN messages
        ON messages.conversation_id = conversations.id
        AND messages.message_id = 
        (
           SELECT MAX(message_id) 
           FROM messages z
           WHERE z.therapist_id_fk = conversations.therapist_id_fk
        )
    WHERE conversations.therapist_id_fk='1'
    GROUP BY conversations.id
    ORDER BY messages.message_id DESC

You want the maximum message for the conversation not the therapist , so I suspect you want this in this subquery: 您想要对话的最大信息而不是治疗师 ,所以我怀疑您希望在此子查询中这样做:

    AND messages.message_id = 
    (
       SELECT MAX(message_id) 
       FROM messages z
       WHERE z.message_id = conversations.id
    )

I'm not sure the outer GROUP BY is correct (it may not be needed at all and it seems to conflict with the columns of the SELECT ), but without sample data and desired results, it is hard to tell. 我不确定外部的GROUP BY是否正确(可能根本不需要它,并且似乎与SELECT的列冲突),但是如果没有示例数据和所需的结果,这很难分辨。

EDIT: 编辑:

I see, the problem is the filtering on therapist id in the outer query. 我知道,问题在于外部查询中对治疗师ID的过滤。 That makes this a bit more complicated: 这使它变得更加复杂:

    AND messages.message_id = 
    (
       SELECT MAX(message_id) 
       FROM messages z JOIN
            conversations c
            ON m.conversation_id = c.id
       WHERE c.therapist_id_fk = 1 AND  -- restriction on subquery
             z.message_id = conversations.id  -- correlation to outer query
    )

Don't use single quotes for constants unless the column is a string or date. 除非列是字符串或日期,否则不要对常量使用单引号。

    SELECT 
        conversations.*,
        messages.message,
        patients.first_name as fullname,
        patients.city,
        patients.thumb,
        patients.gender,
        patients.online_status
    FROM 
        patients
    INNER JOIN conversations
        ON conversations.patient_id_fk = patients.id
    LEFT JOIN messages
        ON messages.conversation_id = conversations.id
        AND messages.message_id = 
        (
           SELECT MAX(message_id) 
           FROM messages z
           WHERE z.conversation_id = conversations.id
        )
    WHERE conversations.therapist_id_fk='1'
    ORDER BY messages.message_id DESC

Found the issue. 找到了问题。 It was here: 在这里:

AND messages.message_id = 
            (
               SELECT MAX(message_id) 
               FROM messages z
               WHERE z.conversation_id = conversations.id
            )

I was using this: 我正在使用这个:

AND messages.message_id = 
        (
           SELECT MAX(message_id) 
           FROM messages z
           WHERE z.therapist_id_fk = conversations.therapist_id_fk
        )

I was able to solve the problem with this query 我可以通过此查询解决问题

SELECT conversations.*,m.message FROM `conversations` LEFT JOIN messages as m on conversations.id = m.conversation_id AND m.id = (SELECT MAX(ms.id) from messages as ms where ms.conversation_id = conversations.id)

I left join the messages and when doing so i select the top most message. 我离开了邮件,这样做时,我选择了最上面的邮件。 Order by can be used to give the order by date Order by可用于按日期给出订单

Your problem is the bit: 您的问题是:

SELECT MAX(message_id) 
FROM messages z
WHERE z.therapist_id_fk = conversations.therapist_id_fk

This query only matches exactly one message. 此查询仅匹配一条消息。 You'd need something a bit different. 您需要一些不同的东西。 Try this: 尝试这个:

SELECT 
    conversations.*,
    messages.message,
    patients.first_name as fullname,
    patients.city,
    patients.thumb,
    patients.gender,
    patients.online_status
FROM 
    patients
INNER JOIN conversations
    ON conversations.patient_id_fk = patients.id
LEFT JOIN messages
    ON messages.conversation_id = conversations.id        
WHERE conversations.therapist_id_fk='1' 
      AND messages.message_id IN (  -- new bit
          SELECT MAX(message_id) FROM messages z GROUP BY z.conversation_id
      )
GROUP BY conversations.id
ORDER BY messages.message_id DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM