子查询中的WHERE子句

时间:2012-10-26 10:49:19

标签: sql select subquery where-clause having-clause

以下SQL查询:

select *  from er_101
 where cd_relnaam IN ( 
     select cd_relnaam 
     from er_101 
     group by cd_relnaam 
     having count(*) > 1)
 AND ld_relopdrachtgever = '1'

虽然我需要将该子查询限制在ld_relopdrachtgever = '1'上 如何通过HAVING语句实现这一点?

2 个答案:

答案 0 :(得分:2)

您还可以在子查询中使用WHERE

SELECT * FROM er_101
 WHERE cd_relnaam IN ( 
     SELECT cd_relnaam 
     FROM er_101 
     WHERE ld_relopdrachtgever = '1'  <--You can add WHERE clause before GROUP BY
   --^^^^^----
     GROUP BY cd_relnaam 
     HAVING COUNT(*) > 1)
 AND ld_relopdrachtgever = '1'

答案 1 :(得分:0)

请参阅此“文章”,了解如何查找重复记录http://blog.sqlauthority.com/2007/07/11/sql-server-count-duplicate-records-rows/