在MySQL中查找重复行,此查询应该失败,但它会返回一些内容:什么?

时间:2014-10-29 22:49:28

标签: mysql

在Oracle中它会失败,因为hr_descr2有十几列,查询不是ANSI有效,但在MySQL上它返回一个结果集,但不是我期望的结果集。我认为它类似于第二个查询返回的结果集,但它看起来更像是一个select *

select *
from hr_descr2 ds2
group by ds2.titolo
having count(1) > 1
order by titolo;

select titolo, count(1)
from hr_descr2 ds2
group by ds2.titolo
having count(1) > 1
order by titolo;

我期待查询执行此操作:

SELECT 
    *
FROM
    hr_descr2
WHERE
    titolo IN (SELECT 
            titolo
        FROM
            hr_descr2 ds2
        GROUP BY ds2.titolo
        HAVING COUNT(1) > 1)
ORDER BY titolo;

1 个答案:

答案 0 :(得分:0)

由于您指出第一个查询有效,我将假设hr_descr2表只有一列:titolo。否则,由于与group by一起使用的非聚合值,它应该抛出错误。

因此,它返回一个重复的titolo个不同值的列表,并告诉您它们出现的次数。