Mysql 如何为SQL中的每个类别显示属于某个类别的数字或行
我有一个视频分类表和一个视频记录表。视频类别中的列为id和cname,视频录制中有一个类别列。我想显示每个类别有多少录像 我试过了Mysql 如何为SQL中的每个类别显示属于某个类别的数字或行,mysql,sql,Mysql,Sql,我有一个视频分类表和一个视频记录表。视频类别中的列为id和cname,视频录制中有一个类别列。我想显示每个类别有多少录像 我试过了 select vc.cname, count(*) as catcount from video_categories vc, video_recordings vr where vc.cname = vr.category group by vc.id 但这只返回id为0的第一个类别的录制数。 我需要它们来显示它们 我创建了这样的表 Video_Recordin
select vc.cname, count(*) as catcount
from video_categories vc, video_recordings vr
where vc.cname = vr.category
group by vc.id
但这只返回id为0的第一个类别的录制数。
我需要它们来显示它们
我创建了这样的表
Video_Recordings (
recording_id,
director,
title,
category,
image_name,
duration,
rating,
year_released,
price,
stock_count
);
Video_Categories (
id,
name
);
Video_Actors (
id,
name,
recording_id
);
使用正确的数据类型,然后使用
Load Data local Infile
' D:/files...' into table
video_recodrings fields terminated by '\t' lines terminated by '\n';
当我这样做并检查表时,表是正确的。嘿,你想要按类别划分行数,然后按分组,按vc.cname分组
select vc.cname, count(*) as catcount
from video_categories vc
inner join video_recordings vr on vc.cname = vr.category
group by vc.cname
您必须按vc.name对它们进行分组
选择vc.cname,count(*)作为catcount
从作为vc的视频类别,到作为vr的视频录制
其中vc.cname=vr.category
按vc.name分组
您的查询工作正常,只需做一点小小的更改。检查您可能将错误的列与视频录制进行了匹配。类别
。我可能错了,但仅返回id=0
这一事实意味着category
列是一个整数,是对Video\u Categories.id
(而不是Video\u Categories.name
)的引用
因此,请根据以下内容更改查询:
select vc.cname, count(*) as catcount
from video_categories vc, video_recordings vr
where vc.id = vr.category
group by vc.id
;
选择vc.cname,count(*)作为catcount
从视频分类vc,视频录制vr
其中vc.id=vr.category
按vc.id分组
;代码>当我这样做时,它仍然只显示1个类别,当有6个类别时没有尾随空格SPLZ使用示例数据添加表结构,因为我认为这不是一个查询问题。我添加了我的表结构组是正确的。数一数正确的事情。不要使用隐式连接。