1

So I'm brand new to writing queries in MySQL, and for some reason I can't grasp the concept very well.

I have a two tables, one called Video_Recordings, and one called Video_Categories. Video_Recordings consists of movie titles with category (genre) names. Video_Categories consists of the names of the different genres.

So, I need to write a query that will display how many films are in each category.

I've entered the query:

select * from Video_Recordings vr, Video_Categories vc
where vr.category=vc.name

Which groups all of the movie titles by category seemingly, but how do I query it to count the number of recordings in each category?

4

1 回答 1

2

使用 将查询按类别分组group by vc.name。然后将 count(*) 添加到选择列表中,该列表会计算每组的行数。

select vc.name, count(*) as NumberOfRecordings
from Video_Recordings vr
inner join Video_Categories vc on vr.category=vc.name
group by vc.name

请注意(至少对于 OP 示例)您不需要加入,因为录制表已经包含类别名称。以下应该产生相同的结果:

select category, count(*) as NumberOfRecordings
from Video_Recordings
group by category
于 2013-10-11T18:46:15.137 回答