SQL:count()的用法 - 混淆和解释

时间:2014-10-24 09:08:42

标签: mysql sql postgresql select count

我正在使用SQL中的以下数据库表(这些条目只是示例):

一个表格,用于保存有关电影的信息:

****************** film ******************
filmid (int)    |   title (string)              | prodyear (int)
-----------------------------------------------------------------
2919898         | Devil May Cry                     | 2011
7246970         | Three Men Seeking Monsters        | 2010

一个表格,用于保存有关电影参与的信息:

****************** filmparticipation ******************
partid (int)    | personid (int) | filmid (int) | parttype (string)
--------------------------------------------------------------------
17              | 17             | 52               | costume designer
85              | 70             | 69               | director`enter code here`

还有一个表格,用于保存有关的信息:

****************** person ******************
personid (int)  | lastname (string) | firstname (string)    | gender (char)
--------------------------------------------------------------------------------
167076          | Jones             | Anthony               | M
197             | Mitchell          | Julia                 | F

现在为问题

1)我试过写一个查询**查找一个人参与过的电影的名字和总数。我的查询如下:

select 
    p.lastname, p.firstname, count(distinct f.filmid) as numberOfMovies
from
    film f, filmparticipation x, person p
where
    x.filmid = f.filmid and
    x.personid = p.personid
group by 
    p.lastname, p.firstname

我相信我遇到了 count()的问题,因为它为每个元组返回1。

2)类似的查询,获取由Christopher Nolan执导的所有电影的影片,参与人数和制作年份(prodyear)。我的查询如下:

select
    f.title, f.filmid, count (distinct x.partid), prodyear
from
    film f, filmparticipation x, person p, filmparticipationinfo inf
where
    p.firstname='Christopher' and
    p.lastname='Nolan' and
    x.personid = p.personid and
    x.parttype = 'director' and
    x.filmid = f.filmid
group by
    f.title, f.filmid, prodyear
order by
    prodyear asc;

同样,在结果中, count()对于每部电影都设置为1,这是不正确的。

如果有人能解释如何正确使用 count(),我们将非常感激。

3 个答案:

答案 0 :(得分:1)

使用JOIN,子查询来获取正确的计数,您的查询应该是:

a)查找某人参与的电影的名称和总数

SELECT 
    p.lastname,
    p.firstname,
    (SELECT COUNT(filmid) FROM filmparticipation WHERE personid = p.personid) AS numberOfMovies
FROM
    person p
GROUP BY
    p.lastname, p.firstname

b)获取由Christopher Nolan执导的所有电影的影片,参与人数和制作年份(prodyear)

SELECT
    f.title,
    f.filmid,
    (SELECT COUNT(partid) FROM filmparticipation WHERE filmid = f.filmid) AS numberOfPeople,
    f.prodyear
FROM
    film f
LEFT JOIN filmparticipation x ON f.filmid = x.filmid
LEFT JOIN person p ON x.personid = p.personid
WHERE
    p.firstname = 'Christopher'
    AND p.lastname='Nolan'
    AND x.parttype = 'director'
GROUP BY
    f.title, f.filmid, f.prodyear
ORDER BY
    f.prodyear ASC;

答案 1 :(得分:0)

我认为这会起作用

select
    p.lastname, p.firstname, count(DISTINCT x.filmid) as numberOfMovies
from
    film f, filmparticipation x, person p
where
    x.personid = p.personid
     and
    x.filmid = f.filmid
group by
    p.lastname, p.firstname

答案 2 :(得分:0)

这是一种方法。第一种情况很简单,您可以使用JOIN和count()来获得结果。在第二种情况下,它有点复杂

select
p.personid,
p.lastname,
p.firstname,
count(distinct fp.filmid) as numberOfMovies
from person p
join filmparticipation fp on fp.personid = p.personid
join film f on f.filmid = fp.filmid
group by p.personid;



select
f.filmid,
f.title,
f.prodyear,
count(*) as total
from film f
join (
  select filmid from filmparticipation fp
  join person p on p.personid = fp.personid
  where p.lastname = 'Christopher' AND p.firstname= 'Nolan' AND fp.parttype = 'director'
)x
on x.filmid = f.filmid
join filmparticipation fp1 on fp1.filmid = f.filmid
group by f.filmid

检查 demo 此处