gpt4 book ai didi

php - MySQL 中两列的不同值

转载 作者:行者123 更新时间:2023-11-29 00:25:21 24 4
gpt4 key购买 nike

我有一个包含如下内容的表格:

+----+-----------+---------+--------+------+
| id | text_from | text_to | text | seen |
+----+-----------+---------+--------+------+
| 1 | A | B | Hello1 | 0 |
| 2 | X | Y | Hello2 | 1 |
| 3 | Y | X | Hello3 | 1 |
| 4 | B | A | Hello4 | 1 |
+----+-----------+---------+--------+------+

这是一个对话,例如 A 向 B 发送文本,B 向 A 发送文本等。如何获得 DISTINCT 对话?例如,A 和 B 或 X 和 Y 之间的不同对话等。

我想得到类似的东西

+----+-----------+---------+--------+------+
| id | text_from | text_to | text | seen |
+----+-----------+---------+--------+------+
| 1 | A | B | Hello1 | 0 |
| 2 | X | Y | Hello2 | 1 |
+----+-----------+---------+--------+------+

如果text_from和text_to一旦有两个唯一值,就不能重复了。例如,如果有text_from = A, text_to = B,则表不应该有text_from = B, text_to = A。

几个小时以来,我一直在尝试 DISTINCT 和 GROUP BY 的几种方法,但找不到任何解决方案!任何建议将不胜感激。

最佳答案

似乎一个简单的 NOT EXISTS 应该可以解决问题。示例 SQL Fiddle

select *
from table t
where not exists (
select 1
from table t1
where
(
(t.text_from = t1.text_from
and t.text_to = t1.text_to)
or (t.text_from = t1.text_to
and t.text_to = t1.text_from)
) and t.id > t1.id
)

关于php - MySQL 中两列的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205198/

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