日期:2014-05-18  浏览次数:20543 次

【求SQL】同表记录的比较?
一张没有主键的表有4个字段:A         B       C       D。
我想查表中有没有A相等,C不等,D相等的记录,
请问怎么写sql??

------解决方案--------------------
select * from tablename a
where exists (
seelct 1 from tablename
where a=a.a
and d=a.d
and c <> a.c
)

------解决方案--------------------
select a.* from table a where a.a in (select a from table where a = a.a and c <> a.c and d = a.d)
------解决方案--------------------

非常感谢几位高手!
我还想请教一下,
如果这个表不是一个实际的表,
而是几张表的联合查询结果,
上面的sql语句应该怎么写?

========================
那就把联合表当作一个表....加上别名就OK了
------解决方案--------------------
和haiwer说的一样 只不过tablename换成@table而已啊