SQL 在和 having 子句中,为什么它们会产生不同的结果?

SQL In and having-clause, why do they produce different results?

考虑到以下关系movie {country,major_genre,production_year, run_time, title,我想列出除西班牙以外的所有国家,该国制作的电影,前提是至少有两个。

我写了两个查询,它们以某种方式产生了不同的结果。似乎第一个是正确的,但在我看来它们是平等的。我正在学习SQL。有人可以帮忙解释一下差异吗?谢谢你的帮助!

第一个:

SELECT m1.country, m1.production_year, m1.title
FROM movie m1
WHERE m1.country <> "Spain"
AND m1.country
IN (
    SELECT m2.country
    FROM movie m2
    GROUP BY m2.country        //select only the ones with at least 2 movies
    HAVING COUNT( * ) >=2
 )
ORDER BY m1.country ASC , m1.production_year DESC 

第二个:

SELECT m1.country, m1.production_year, m1.title
FROM movie m1
WHERE m1.country <> "Spain"
GROUP BY m1.country
HAVING COUNT( * ) >=2  //the country selected should have count of at least 2 rows
ORDER BY m1.country ASC , m1.production_year DESC

问题是第二个查询在单个列上使用 GROUP BY,但 returns 3 列。因此,每个拥有 > 1 部电影的国家都会出现一次,年份和电影的 "random" 值(实际上可能不是随机的)。

你也可以不用 GROUP BY:

SELECT m1.country, m1.production_year, m1.title
FROM movie m1
WHERE m1.country <> "Spain"
AND 1 < (
    SELECT count(*) 
    FROM movie m2
    WHERE m2.country = m1.country
  )
ORDER BY m1.country ASC , m1.production_year DESC 

这是一个fiddle:http://sqlfiddle.com/#!9/e2ddc/2