1

这是我的数据库的样子:

桌子:conversations

+----+--------+--------+
| id | user_1 | user_2 |
+----+--------+--------+
| 1  | 1      | 2      |
| 2  | 2      | 3      |
| 3  | 1      | 3      |
+----+--------+--------+

桌子:messages

+----+--------------+------+
| id | conversation | text |
+----+--------------+------+
| 1  | 1            | hej  |
| 2  | 1            | test |
| 3  | 2            | doh  |
| 4  | 2            | hi   |
| 5  | 3            | :)   |
| 6  | 3            | :D   |
+----+--------------+------+

然后当我运行以下查询时:

SELECT
    *
FROM `messages`
INNER JOIN `conversations`
    ON `conversations`.`id` = `messages`.`convesations`
GROUP BY `conversations`.`id`
ORDER BY `messages`.`id` DESC

然后我把那些从messages

+----+--------------+------+
| id | conversation | text |
+----+--------------+------+
| 1  | 1            | hej  |
| 3  | 2            | doh  |
| 5  | 3            | :)   |
+----+--------------+------+

但是,是否有可能以某种方式让我messages.id从该组中获得最高的消息,而不是最低的?

编辑:这是我想要的输出messages

+----+--------------+------+
| id | conversation | text |
+----+--------------+------+
| 2  | 1            | test |
| 4  | 2            | hi   |
| 6  | 3            | :D   |
+----+--------------+------+

因为那些与最高messages的相同。conversationid

4

5 回答 5

7
SELECT  *
FROM    conversations c
JOIN    messages m
ON      m.id =
        (
        SELECT  id
        FROM    messages mi
        WHERE   mi.conversation = c.id
        ORDER BY
                mi.conversation DESC, mi.id DESC
        LIMIT 1
        )

为此创建一个索引以messages (conversation, id)使其快速工作。

于 2013-04-26T11:29:11.010 回答
4

您只需要像这样使用嵌套查询:

SELECT * FROM Messages 
WHERE ID IN(
            SELECT Max(m.ID) FROM Messages m
            INNER JOIN conversations c
                    ON c.id = m.conversation
              GROUP BY m.conversation
           );

输出:

| ID | CONVERSATION | TEXT |
----------------------------
|  2 |            1 | test |
|  4 |            2 |   hi |
|  6 |            3 |   :D |

如果您想要两个表中的数据,请尝试以下操作:

SELECT * FROM Messages m
    JOIN conversations c
      ON c.id = m.conversation
WHERE m.ID IN (
                 SELECT MAX(ID) FROM Messages 
                 GROUP BY conversation
              )
GROUP BY m.conversation;

输出:

| ID | CONVERSATION | TEXT | USER_1 | USER_2 |
----------------------------------------------
|  2 |            1 | test |      1 |      2 |
|  4 |            2 |   hi |      2 |      3 |
|  6 |            3 |   :D |      1 |      3 |

看到这个 SQLFiddle

于 2013-04-26T11:31:56.347 回答
1

我认为你只是有一个不正确的表连接:

SELECT *
FROM `messages`
INNER JOIN `conversations`
    ON `conversations`.`id` = `messages`.`conversation`
GROUP BY `conversations`.`id`
ORDER BY `messages`.`id` DESC

编辑

你可以试试这个:

SELECT *
FROM `messages`
WHERE `messages`.`id` IN (
    SELECT MAX(id)
    FROM messages
    GROUP BY conversation
)
于 2013-04-26T11:26:23.023 回答
1

您在错误的列上加入。对话中的“Id”不能等于消息中的“Id”。

我认为,表格消息中的“对话”是“id_conversation”,对吗?

所以,如果我理解得很好:

SELECT *
FROM messages
INNER JOIN conversations
    ON conversations.id = messages.conversation
GROUP BY conversations.id
ORDER BY messages.id DESC
于 2013-04-26T11:32:05.140 回答
1

几种不同的方法:

这种方法依赖于 MySQL 中已知但未记录的行为,其中在分组查询中返回的未聚合、未分组的值是排序顺序中的第一个 - 它很快,但不应被视为可靠:

SELECT * FROM 
(SELECT * FROM messages 
ORDER BY conversation, id desc) a
GROUP BY conversation

或者,一种应该始终可靠的方法:

SELECT m.*, c.user_1, c.user_2 FROM messages m
 JOIN (select conversation, max(id) max_id from messages group by conversation) l
   ON m.id = l.max_id
 JOIN conversations c
   ON c.id = m.conversation
GROUP BY conversation

SQLFiddle在这里

于 2013-04-26T11:41:33.697 回答