gpt4 book ai didi

MySQL:按两列分组而不考虑顺序

转载 作者:行者123 更新时间:2023-11-29 03:22:39 25 4
gpt4 key购买 nike

我有一个表格,就像短信一样,它有 fromtomessagetime 列(除其他外)。我正在尝试询问两个数字(fromto)之间的最新记录,不考虑方向

这是表格的一个例子:

+--------------+-----------------+--------------------+--------------+
| from | to | message | time |
+--------------+-----------------+--------------------+--------------+
| 555-1234 | 555-9876 | I'll be there. | 06:00 |
| 555-9876 | 555-5555 | message3 | 05:30 |
| 555-9876 | 555-1234 | Bring beer | 05:00 |
| 555-9876 | 555-1234 | My place at 8pm | 04:00 |
| 555-9876 | 555-5555 | message2 | 03:45 |
| 555-5555 | 555-9876 | message1 | 03:30 |
| 555-9876 | 555-1234 | Are you coming? | 03:00 |
| 555-1234 | 555-9876 | Yeah, what's up? | 02:00 |
| 555-9876 | 555-1234 | Are you there? | 01:00 |
+--------------+-----------------+--------------------+--------------+

我只想从此示例中获取以下记录:

+--------------+-----------------+--------------------+--------------+
| from | to | message | time |
+--------------+-----------------+--------------------+--------------+
| 555-1234 | 555-9876 | I'll be there. | 06:00 |
| 555-9876 | 555-5555 | message3 | 05:30 |
+--------------+-----------------+--------------------+--------------+

我如何要求这些?

最佳答案

你可以使用带最大值和元组的子查询

select * from my_table 
where (from, to, time) in ( select from, to, max(time)
from my_table
group by from, to)

关于MySQL:按两列分组而不考虑顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41313477/

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