查询时SQL重复结果

时间:2014-04-05 19:34:57

标签: sql sqlite duplicates duplicate-data

因此,我尝试使用以下SQL语句从相对较小的数据库返回结果。我已检查数据库中的重复条目,但所有条目都不同。代码如下:

SELECT *
  FROM cds
       JOIN releases
         ON cds.cd_id = releases.cd_id
       JOIN bands
         ON releases.band_id = bands.band_id
 WHERE position = '1' 
       AND
       band_name = 'Prodigy';

还有这个...

SELECT title, position 
    FROM cds INNER JOIN releases ON cds.cd_id = releases.cd_id
    WHERE position = '1' 
    AND band_id = '3'

为什么我会收到重复的结果?

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT DISTINCT
    title, position 
FROM cds INNER JOIN releases ON cds.cd_id = releases.cd_id
WHERE position = '1' 
AND band_id = '3'

对上面的查询也一样。只需确保只有SELECT您实际需要的列。

您可以运行这些查询以查看获取重复数据的原因

SELECT
    cd_id,
    COUNT(1) AS Amount
FROM [releases]
GROUP BY cd_id
HAVING COUNT(1) > 1
ORDER BY Amount DESC, cd_id ASC;

该查询中出现的所有内容都是您拥有重复数据的原因。