在MySQL中选择重复记录

时间:2016-07-29 06:01:32

标签: mysql sql-server

此查询适用于SQL Server但在MySQL中它不起作用。有人可以帮忙吗?

SELECT name 
FROM table_name 
WHERE 
   Institution_Code =  1  and 
   Month= 6 and Year= 2016 and 
  Id IN (
          SELECT Id FROM table_name 
          where Institution_Code =  1 and 
                Month= 6 and Year= 2016 
          GROUP BY Id HAVING count(Id) > 1 
        )

3 个答案:

答案 0 :(得分:0)

尝试转义查询中的保留字:

SELECT name 
FROM table_name 
WHERE Institution_Code = 1 
and `Month`= 6 
and `Year`= 2016 
and Id IN (
    SELECT Id 
    FROM table_name 
    where Institution_Code = 1 
    and `Month`= 6 
    and `Year`= 2016 
    GROUP BY Id 
    HAVING count(Id) > 1 )

答案 1 :(得分:0)

这样做:

SELECT name 
FROM table_name 
WHERE Institution_Code = 1 
and [Month]= 6 
and [Year]= 2016 
and Id IN (
    SELECT Id 
    FROM table_name 
    where Institution_Code = 1 
    and [Month]= 6 
    and [Year]= 2016 
    GROUP BY Id 
    HAVING count(Id) > 1 )

SQL保留字必须包含在此括号中[此处为保留字]

答案 2 :(得分:0)

你可能会试试这个

SELECT * , count( 'name' ) is_duplicate
FROM `tbl_name`
GROUP BY `name`
HAVING is_duplicate >1