select u.UserName,u.UserGrade,count(o.OrderId) as ordercount,count(o.price) as pricecount 
from userinfo as u inner JOIN orders as o on u.UserName=o.UserName where u.UserName= 'admin' and o.state=0 
group by u.UserName,u.UserGrade

解决方案 »

  1.   

    给表结构,给几条测试数据,和你要得到的结果建议发贴前阅读
    http://topic.csdn.net/u/20071023/21/997c7e68-6d8f-43c1-aa3c-0bccfb9293d0.html
      

  2.   

    把显示的列(没有count/sum/max/min)做为group by 就行了:如
    select col1,count(col2),max(col2),min(col2),sum(col2)from t group by col1
      

  3.   

    count(o.OrderId) as ordercount,count(o.price) as pricecount-------------这两个count返回值会不一样吗?和直接count(*)没有任何区别。不知道楼主要什么数据,group by楼上写对了,数据恐怕不是期望的数据。
      

  4.   

    是的,Limpire 好细心啊,count(o.OrderId),count(o.price)分别取某个用户的未处理的订单,未处理的订单价格总合
      

  5.   

    select u.UserName,u.UserGrade,count(o.OrderId) as ordercount,sum(o.price) as pricecount 
    from userinfo as u inner JOIN orders as o on u.UserName=o.UserName where u.UserName= 'admin' and o.state=0 
    group by u.UserName,u.UserGrade 
    谢谢,揭帖了
      

  6.   

    将其中非统计列作为group by 后的列即可select u.UserName,u.UserGrade,count(o.OrderId) as ordercount,count(o.price) as pricecount from userinfo as u inner JOIN orders as o on u.UserName=o.UserName where u.UserName= 'admin ' and o.state=0 
    group by u.username,u.usergrade
      

  7.   

    select u.UserName,u.UserGrade,count(o.OrderId) as ordercount,sum(cast(o.price as decimal)) as pricecount from userinfo as u inner JOIN orders as o on u.UserName=o.UserName where u.UserName= 'admin ' and o.state=0 
    group by u.username,u.usergrade