日期:2014-05-18 浏览次数:20877 次
select a.aid,b.bid,count(c.cid) cnt from a inner join b on b.aid=a.aid inner join c on c.bid=b.bid group by a.aid,b.bid
------解决方案--------------------
select AID,Bid,COUNT(*) as Num from A left join C on A.AID = C.AID group by BID
希望有用~
------解决方案--------------------
要显示,a.title,b.title,需要inner join A表和B表
inner join A on A.AID=C.AID
inner join B on B.BID=C.BID
另外使用Group by时的要求就是,除了聚合函数,select中的字段必须出现在group by中。
意思就是要select a.title,b.title 就必须group by a.title,b.title
完整的句子你自己可以试着拼一下。SQL Server联机丛书里面应该都有例子。
------解决方案--------------------
我怕你调不出来 我帮你调吧 逻辑是对的我用以前我的投票表给你做了示范下面的你只要把字段和表改一下就OK了。。
select ab.atitle,ab.btitle,count(c.ID) as num from(select a.ID as aid,b.ID as bid, a.VoteTitle as atitle ,b.VoteAnswer as btitle from VoteEvent as a left join VoteOption as b on a.ID=b.VoteID group BY a.ID,b.ID,a.VoteTitle,b.VoteAnswer )as ab left join VoteDetail as c on c.VoteOpID=ab.bid group by ab.atitle,ab.btitle
语句结尾可以写个order by ab.atitle 这样排一下 因为我在做查询的时候数据发现数据出来了 可是有点乱