Mysql 编写带有计数的SQL查询
我有两张桌子 类别: 猫号,猫名 主题: 主题id、类别id、主题名称 我想获得所有cat_名称的列表以及topics表中针对特定cat_id的主题计数Mysql 编写带有计数的SQL查询,mysql,sql,Mysql,Sql,我有两张桌子 类别: 猫号,猫名 主题: 主题id、类别id、主题名称 我想获得所有cat_名称的列表以及topics表中针对特定cat_id的主题计数 Category Topics ---------------------- ABC 2 CDE 5 非常感谢您的快速帮助 谢谢尝试此查询 select a.cat_name as category, count(*) as Topics from category a join T
Category Topics
----------------------
ABC 2
CDE 5
非常感谢您的快速帮助
谢谢尝试此查询
select a.cat_name as category, count(*) as Topics
from category a
join Topics b on a.cat_id=b.cat_id
group by a.cat_name
请尝试此查询
select a.cat_name as category, count(*) as Topics
from category a
join Topics b on a.cat_id=b.cat_id
group by a.cat_name
试试这个
select cat_name, count(*) as counting
from Category as cat
inner join Topics as t on cat.cat_id=t.cat_id
group by cat_name
试试这个
select cat_name, count(*) as counting
from Category as cat
inner join Topics as t on cat.cat_id=t.cat_id
group by cat_name
试试这个
SELECT Category.cat_name as Category,
(SELECT COUNT(topic_id) FROM Topics
WHERE Topics.cat_id = category.cat_id) AS Topics
FROM Category
试试这个
SELECT Category.cat_name as Category,
(SELECT COUNT(topic_id) FROM Topics
WHERE Topics.cat_id = category.cat_id) AS Topics
FROM Category
这是您自己在评论中显示的查询:
select
category.cat_id as id,
category.cat_name as category,
count(select * from topics where topics.topic_id=id) as topics
from category
inner join topics on category.cat_id=topics.cat_id;
首先是语法错误。而不是
count(select * from topics where topics.topic_id=id) as topics
这必须是
(select count(*) from topics where topics.topic_id=id) as topics
然后,您将读取topics表两次,一次在联接中,一次在子查询中。所以这看起来像是在一个语句中混合了两次尝试。以下是两个分开的选项:
select
category.cat_id as id,
category.cat_name as category,
(select count(*) from topics where topics.topic_id = category.id) as topics
from category;
或:
这是您自己在评论中显示的查询:
select
category.cat_id as id,
category.cat_name as category,
count(select * from topics where topics.topic_id=id) as topics
from category
inner join topics on category.cat_id=topics.cat_id;
首先是语法错误。而不是
count(select * from topics where topics.topic_id=id) as topics
这必须是
(select count(*) from topics where topics.topic_id=id) as topics
然后,您将读取topics表两次,一次在联接中,一次在子查询中。所以这看起来像是在一个语句中混合了两次尝试。以下是两个分开的选项:
select
category.cat_id as id,
category.cat_name as category,
(select count(*) from topics where topics.topic_id = category.id) as topics
from category;
或:
使用GROUPBY子句
使用GROUPBY子句
选择category.cat\u id作为id,category.cat\u name作为category,countselect*from topics where topics.topic\u id=id作为category内部的主题在category上加入topics.cat\u id=topics.cat\u id您应该编辑您的问题,这比在注释中编写代码要好得多,更具可读性…Aleksandar Miladinovic-仍在学习:选择category.cat_id作为id,category.cat_name作为category,countselect*从主题所在的主题。topic_id=id作为主题从类别内部加入主题。cat_id=topics.cat_i你应该编辑你的问题,这比在注释中编写代码要好得多,它更具可读性…Aleksandar Miladinovic-仍在学习中:这只适用于至少有一个主题的类别。左连接执行作业这只适用于至少有一个主题的类别。左连接执行错误的GROUP BY子句。它应该是每个类别名称的一个结果行,而不是每个主题id。@ThorstenKettner感谢您的更正。。。我没有注意到:按子句分组是错误的。它应该是每个类别名称的一个结果行,而不是每个主题id。@ThorstenKettner感谢您的更正。。。我没有注意到:更好!现在从主查询的列表中删除主题。更好!现在从主查询的from列表中删除主题。