日期:2014-05-17  浏览次数:20582 次

基础问题,关于嵌套查询的
就是订单,用户表,两个表join查根据userid分组统计的订单数

select user.userid,count(opportunityID) as 订单总数 from UserTable as [user]
join Business_Opportunitys as opportunity
on user.userid = opportunity.userid
group by user.userid

现在我统计出了一个id名下的所有订单了。

呃,现在我想要再写一个查询,类似

select username,count(opportunityID) from UserTable where userid in(
select user.userid,count(opportunityID) as 订单总数 from UserTable  as [user]
join Business_Opportunitys as opportunity
on user.userid = opportunity.userid
group by user.userid
)

目的是根据id来查姓名,同时也查到name下的订单总数。(就是把id变成name而已)
显然,上面那个是错的句子查不出来,所以想问问大神们这个东西怎么玩。。
当然,分成两个句子查就不用说了,我只是想了解一下有没有这样的用法
sql join

------解决方案--------------------
引用:
引用:
有重名,就把ID带上,ID是唯一的吧
带上ID?呃,怎么写。。


你的name肯定对应一个唯一字段,用left join啊,然后把那个唯一字段挂出来就行
------解决方案--------------------
引用:
引用:引用:引用:引用:引用:SQL code?123456select username,count(opportunityID) from UserTable where u……

赶紧写你的语句吧