我是SQL新手.我有一个MySQL表,其中的行如下
a | b | c | d | e | f |
---|---|---|---|---|---|
abra | ca | da | bra | sa | 7 |
abra | fa | ll | of | mo | 8 |
ac | st | da | bra | sa | 10 |
ac | ca | da | bra | mo | 9 |
pa | ni | rar | pat | sa | 11 |
pa | pu | bra | sa | mo | 11 |
列e
中的值是sa
或mo
.对于列a
的每个值,总是有两行,一行是sa
,另一行是mo
(加上其他几行).
我想要获取列a
值相同但第sa
行和mo
行在其列f
中具有不同值的所有行.因此,对于上面的示例,查询将过滤掉最后两行,因为它们在列f
中都有值11
,并且将返回
a | b | c | d | e | f |
---|---|---|---|---|---|
abra | ca | da | bra | sa | 7 |
abra | fa | ll | of | mo | 8 |
ac | st | da | bra | sa | 10 |
ac | ca | da | bra | mo | 9 |
我try 了这个查询,但没有返回正确的数据
select * from table t1 where exists
(select 1 from table t2 where t1.a <> ''
and t2.a <> '' and t1.a=t2.a and (t2.e='sa' or t2.e='mo') and t1.e=t2.e and t1.f<>t2.f) order by a