你那样写应该是m.Merchant_ = osl.MerchantID和m.Merchant_ = cbl.MerchantID 这两个结果之积的基础上再按m.Merchant_ 分组,你要计算osl中的每个merchant_的count数,为什么要把cbl这个表也加上去呢。select count(osl.MerchantID) as Impression from Merchant m, Log osl where m.Merchant_ = osl.MerchantID group by m.Merchant_ 这个不是所要得结果么
如果你的sql版本支持子查询的话,也可以实现把os1和cb1的某一个m.Merchant_分组的结果在返回的某一条结果集里显示出来,不支持的话要这样恐怕比较难了,不过可以用union all把两种情况的结果合并起来,在加个表名区分是哪个表的分组统计记录: select 'os1' as tablename,m.Merchant_,count(osl.MerchantID) as Impression from Merchant m, Log osl where m.Merchant_ = osl.MerchantID group by m.Merchant_ union all select 'cb1' as tablename,m.Merchant_,count(cbl.MerchantID) as Impression from Merchant m, Click cbl where m.Merchant_ = cbl.MerchantID group by m.Merchant_; 这样也应该能清楚的显示分组结果集
sum和count一样,结果也是osl和cbl的sum,指定某个表没有作用,为什么?
加上去的目的是还要计算cbl.merchant的count数,想在一条语句里解决,有没有办法了,请指教。
select 'os1' as tablename,m.Merchant_,count(osl.MerchantID) as Impression from Merchant m, Log osl where m.Merchant_ = osl.MerchantID group by m.Merchant_ union all select 'cb1' as tablename,m.Merchant_,count(cbl.MerchantID) as Impression from Merchant m, Click cbl where m.Merchant_ = cbl.MerchantID group by m.Merchant_;
这样也应该能清楚的显示分组结果集