gpt4 book ai didi

mysql - 如何在mySQL中选择等效但倒排的元组

转载 作者:行者123 更新时间:2023-11-29 07:47:44 25 4
gpt4 key购买 nike

我有一个包含 2 列的表格,其中填充了数值:

Column1     Column2
1 2
6 9
2 1
9 6
1 3

元组(x,y)相当于我的问题领域中的元组(y,x),我需要执行一个选择,我只得到其中之一。意思是,我想要一个返回的选择查询:

Column1   Column2
1 2
6 9
1 3

在 mySQL 中实现此目的的最佳方法是什么?

最佳答案

试试这个:

select t.*
from table t
where column1 < column2 or
not exists (select 1
from table t2
where t2.column1 = t.column2 and t1.column2 = t.column1
);

如果你只想要成对的,即使它们不在原始表中,那么你可以这样做:

select least(column1, column2), greatest(column1, column2)
from table t
group by least(column1, column2), greatest(column1, column2)

关于mysql - 如何在mySQL中选择等效但倒排的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27262864/

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