PHP/SQL在一列中查找重复行,条件是另一列不同
我需要SQL查询来执行以下任务: 我有两个专栏。列“foo”和列“bar” 当且仅当列“foo”具有不同的值而“bar”具有相同的值时,查询才需要返回结果 例如:PHP/SQL在一列中查找重复行,条件是另一列不同,php,mysql,sql,database,Php,Mysql,Sql,Database,我需要SQL查询来执行以下任务: 我有两个专栏。列“foo”和列“bar” 当且仅当列“foo”具有不同的值而“bar”具有相同的值时,查询才需要返回结果 例如: Foo Bar --------------------- 1 John 1 Lee 2 James 3 Robin <- the value '3' needs to
Foo Bar
---------------------
1 John
1 Lee
2 James
3 Robin <- the value '3' needs to be returned
3 Sally
1 Peter
1 John
4 Brian
2 Robin <- the value '2' needs to be returned
如果我要在上面的数据集上运行查询,那么上面用E箭头指示的两行都将返回,因为两行上的“bar”相同,但“foo”不同
任何帮助都将不胜感激
谢谢。您可以使用exists执行您想要的操作:
似乎不起作用。它只是说t不存在。。。。对不起,我是这方面的新手…@user3931836。这是您的表名。@user3931836。它是表名子查询中的别名。
select t.*
from t
where exists (select 1
from t t2
where t2.bar = t.bar and t2.foo <> t.foo
);