日期:2014-05-19  浏览次数:20462 次

求一个关于两个字段过滤的SQL语句,谢谢!
比如说有这样一张表Table1:
字段1:记录代码
字段2:项目代码
字段3:角色代码
其中记录代码是主键。

例如有这样的记录:

记录代码   项目代码   角色代码
a01             07               03
a02             07               03
a03             08               09
a04             04               09
a05             04               09

现在想查找出满足这样条件的记录:项目代码和角色代码相同的记录。
即:最后的查询结果应该是上表的a01、a02、a04、a05这四条记录。

请问该如何来写这个SQL语句?
谢谢!




------解决方案--------------------
select * from table1 a where exists(select 1 from table 1 where
记录代码=a.记录代码
group by 项目代码,角色代码 having count(1)> 1)

------解决方案--------------------
select
a.*
from
Table1 a
where
exists(select
1
from
Table1
where
记录代码!= a.记录代码
and
项目代码 = a.项目代码
and
角色代码 = a.角色代码)