我想选择 column1
中具有相同值的行和column2
但不同之处在于 column3
.
这是我的 table 。
这是我想要的输出。
这是我尝试过的。
SELECT column1, column2
FROM my_table
GROUP BY column1, column2, column3
HAVING COUNT(column1) > 1;
最佳答案
I want to select rows that have the same value in column1 and column2 but differ in column3.
使用存在
:
select t.*
from my_table t
where exists (select 1
from my_table t2
where t2.column1 = t.column1 and t2.column2 = t.column2 and
t2.column3 <> t.column3
);
不过,您的示例结果表明您只需要 column1
/column2
对。如果是这样:
SELECT column1, column2
FROM my_table
GROUP BY column1, column2
HAVING MIN(column3) <> MAX(column3);
您的查询版本仅返回 column1
/column2
对,其中 column3
值包含多行。
关于mysql - SELECT 行在两个字段中具有相同的值,而在另一个字段中具有不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67579045/