Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL:count()的用法-混淆和解释_Mysql_Sql_Postgresql_Select_Count - Fatal编程技术网

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

Mysql SQL:count()的用法-混淆和解释,mysql,sql,postgresql,select,count,Mysql,Sql,Postgresql,Select,Count,我正在使用SQL中的以下数据库表(条目只是示例): 一个表格用于保存有关胶片的信息: ****************** film ****************** filmid (int) | title (string) | prodyear (int) ----------------------------------------------------------------- 2919898 | Devil May Cry

我正在使用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
和一个表格,用于保存有关人的信息:

****************** 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
现在来看问题

****************** 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)导演的所有电影的电影ID、参与人数和制作年份(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(),我们将不胜感激

使用联接、子查询来获得正确的计数,您的查询应该是:

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
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;
b)获取克里斯托弗·诺兰导演的所有电影的电影ID、参与人数和制作年份(prodyear)

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
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;
我想这会管用的

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

这里有一个方法。第一种情况很简单,可以使用JOIN with 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

检查此处的

首先,您应该了解有关联接的更多信息。其次,要获得正确的计数,需要使用子查询。LEFT JOIN x。。。其中x…-有点困惑?一个左[OUTER]连接和一个WHERE子句(作用于同一个表上)结合在一起会被翻译成一个[INNER]连接。。。其中x不为NULL转换为内部联接。。。嗯,你能指着docu吗?为什么不自己测试一下呢?一个简单的解释加上显示警告就足够了