Symfony - 获取最新的对话消息

时间:2017-09-18 09:08:48

标签: mysql symfony doctrine greatest-n-per-group

我有一个用户实体,在这样的消息实体中有两个用户外键(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”的每个最后记录(不同的 from_id to_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

感谢大家的帮助:)

1 个答案:

答案 0 :(得分:1)

要从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

编辑获取用户&#34; 2&#34;的最后记录(与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