MySQL选择互惠的记录对,没有重复 [英] Mysql Select reciprocal pairs of records, without duplicates

查看:63
本文介绍了MySQL选择互惠的记录对,没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的表:

I have a table with two columns:

  1. person_id
  2. 第一字段ID与之合作的person_id

我需要选择所有合作对,这很容易,但是出了什么问题: 表格中的数据如下:987-102103 - 104104 - 10321 - 102. 由于具有此类数据的结果,我应该具有3个合作对987 - 102103-10421-102,就像您看到的103 - 104104 - 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选择互惠的记录对,没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆