SQL 表连接的问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 19:12:16
A表存有文章信息
B表是文章的评论信息

现在要取出一篇文章同时取出B表中所有对该文章的评论的总数

即把
select * from A where aid=@id
select count(*) from B where aid = @id
作为一条记录输出,怎么写?

select A.*,B.counts
from (select count(*)as counts from B where aid = @id ) B,A
where A.aid = B.aid

用联合查询就可以很简单的实现了!

你把字段写出来,我帮你写!

select A.* ,C.评论总数
from A,(select B.aid ,count(*) as 评论总数 from B group by aid) as C
where a.aid=c.aid and a.aid=@id

测试通过