sql gtoup by 用法?

来源:百度知道 编辑:UC知道 时间:2024/05/29 00:25:51
2个表,一个主贴,一个回复,我要获取主贴有多少回复和获得主贴表的内容应该如何写?
我按以下写法他说我的 类名‘回复数量’无效
select count(Comments.ArticleId) as 回复数量,Articles.*
from Articles
INNER JOIN Comments ON Articles.Id = Comments.ArticleId
where Articles.Id=1
group by sa
打错,是group by .

select c.*,T.回复数量 from Articles c,(select b.Id,count(a.ArticleId) as 回复数量 from Comments a,Articles b where a.ArticleId=b.id group by b.id) T where T.id=c.id

你的Articles 表里都有什么字段?你查询Articles.*肯定是不行了。你最后只group by sa一个字段呢

你把要查的列名明确的列一下

----补充----
select a.主贴名,a.主贴id,count(*) as 回复数量 from Articles a,Comments b
where Articles.Id = Comments.ArticleId
and Articles.Id=1
group by a.主贴名,a.主贴id

给你写个类似的,你照葫芦画瓢写一下吧

楼上回答的是正确的。我补充解释一下,group by 一般是和COUNT(),SUM(),AVG()函数一并使用,是分组,关键是你要按什么分组统计,比如要按照学生性别分组,统计人数和平均成绩:
select 性别,avg(总成绩) as 平均成绩,count(性别) as 人数 FROM 表 group by 性别