问题描述
我有一个包含两列的表:
I have a table with two columns:
- person_id
- 第一字段ID与之合作的person_id
我需要选择所有合作对,这很容易,但是出了什么问题:表格中的数据如下:987
-102
,103 - 104
,104 - 103
,21 - 102
.由于具有此类数据的结果,我应该具有3个合作对987 - 102
,103-104
,21-102
,就像您看到的103 - 104
和104 - 103
记录具有相同的逻辑一样,我如何避免重复它们.有什么主意吗?
I need to select all cooperation pairs, it is easy but in what is the problem:table have data like: 987
- 102
, 103 - 104
, 104 - 103
, 21 - 102
. As the result with such data i should have 3 cooperation pairs 987 - 102
, 103-104
, 21-102
, as you see 103 - 104
and 104 - 103
records have the same logic, how can I avoid duplicating of them. Any idea?
谢谢,并致以最诚挚的问候.安东.
Thanks, and best regards. Anton.
推荐答案
您可以使用MySQL的 LEAST()
和 GREATEST()
函数以及DISTINCT
:
You could use MySQL's LEAST()
and GREATEST()
functions, together with DISTINCT
:
SELECT DISTINCT LEAST(a, b), GREATEST(a, b) FROM mytable
这篇关于MySQL选择互惠的记录对,没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!