gpt4 book ai didi

mysql - Symfony - 获取最新的对话信息

转载 作者:行者123 更新时间:2023-11-28 23:09:51 25 4
gpt4 key购买 nike

我有一个用户实体,在这样的消息实体中有两个用户外键(from_id 和 to_id):

所以,所有的记录都是:

id     | from_id | to_id  |     datemessage        |  message        |
:------|:--------|:-------|:-----------------------|:----------------|
10 | 2 | 1 | 2017-09-17 18:45:46 | redsfdffdfgd
13 | 2 | 1 | 2017-09-18 13:33:18 | Hi You
15 | 2 | 1 | 2017-09-18 13:33:52 | Hi You
17 | 2 | 1 | 2017-09-18 13:34:41 | Hi You
5 | 4 | 2 | 2017-09-17 09:27:04 | TEst
8 | 1 | 2 | 2017-09-18 15:01:03 | testasdfas
9 | 1 | 3 | 2017-09-17 18:42:34 | jjkjlkjkjkl
14 | 2 | 3 | 2017-09-18 13:33:32 | Hi You
16 | 2 | 3 | 2017-09-18 13:34:30 | Hey You
6 | 2 | 4 | 2017-09-17 10:14:31 | test
7 | 2 | 4 | 2017-09-17 10:14:57 | a bon voila
11 | 1 | 4 | 2017-09-18 13:32:20 | Hey You
12 | 2 | 4 | 2017-09-18 13:32:57 | Hey ZIoup
18 | 2 | 4 | 2017-09-18 13:34:51 | Have a nice day

我想要的用户 2 的结果是:

id     | from_id | to_id  |     datemessage        |  message        |
:------|:--------|:-------|:-----------------------|:----------------|
8 | 1 | 2 | 2017-09-18 15:01:03 | testasdfas
16 | 2 | 3 | 2017-09-18 13:34:30 | Hey You
18 | 2 | 4 | 2017-09-18 13:34:51 | Have a nice day

我想要用户“2”的所有last 记录(不同的from_idto_id)感谢大家可以帮助我:)

我试过这个查询,但它没有按日期对我的查询进行排序:

SELECT distinct_id,
id,
from_id,
to_id,
datemessage,
message,
FROM
(SELECT message.from_id AS from_id,
message.id AS id,
message.from_id AS distinct_id,
message.to_id AS to_id,
message.datemessage AS datemessage,
message.message AS message,
fos_user.id AS user_id,
fos_user.username AS username
FROM message
LEFT JOIN fos_user ON message.from_id = fos_user.id
WHERE message.to_id = :user_id
UNION SELECT message.to_id AS to_id,
message.id AS id,
message.to_id AS distinct_id,
message.from_id AS to_from,
message.datemessage AS datemessage,
message.message AS message,
fos_user.id AS user_id,
fos_user.username AS username
FROM message
LEFT JOIN fos_user ON message.to_id = fos_user.id
WHERE message.from_id = :user_id ) AS message
GROUP BY distinct_id
ORDER BY DATE(datemessage) ASC

感谢大家的帮助:)

最佳答案

要从 from_id = 1 的对话中选择最新消息,您可以使用以下查询

select m.*
from
message m
left join message m1 on (m.from_id = m1.from_id
and m.to_id = m1.to_id
and m.messagedate < m1.messagedate)
where m1.id is null
and m.from_id = 1;

要在查询中显示名称,您可以对用户表进行左连接

select m.*,u.username,u1.username
from
message m
left join message m1 on (m.from_id = m1.from_id
and m.to_id = m1.to_id
and m.messagedate < m1.messagedate)
left join fos_user u on(m.from_id = u.id)
left join fos_user u1 on(m.to_id = u1.id)
where m1.id is null
and m.from_id = 1;

DEMO

编辑 获取用户“2”的最后记录(不同于 from_id 和 to_id)

select m.*
from
message m
left join message m1 on (
(
(m.from_id = m1.from_id and m.to_id = m1.to_id)
or
(m.from_id = m1.to_id and m.to_id = m1.from_id )
)
and m.datemessage < m1.datemessage
)
where m1.id is null
and (m.from_id = 2 or m.to_id = 2)
order by m.id

Updated Demo

关于mysql - Symfony - 获取最新的对话信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com