表:xiaoshou
 字段:id ,username
 数据:1    张三
       2    李四
表:dingdan
 字段:id ,ordertotal
      1     100
      1      50
 
sql:select username,sum(ordertotal) as ordertotal from xiaoshou left outer join dingdan on xiaoshou.id=dingdan.id where ordertotal>0 group by username
 
查询结果:username   ordertotal
            张三         150希望得到的结果:username  ordertotal
              张三        150
              李四        0
即使“李四”没有数据也要显示李四的名字,且ordertotal为0。多谢!!

解决方案 »

  1.   


    你都加了where ordertotal>0这个限制条件了,当然只显示ordertotal大于0的
      

  2.   

    select username,isnull(sum(ordertotal),0) as ordertotal from xiaoshou left outer join dingdan on xiaoshou.id=dingdan.id group by username
      

  3.   


    mysql> select * from xiaoshou;
    +------+----------+
    | id   | username |
    +------+----------+
    |    1 | zhangsan |
    |    2 | lisi     |
    +------+----------+
    2 rows in set (0.00 sec)mysql> select * from dingdan;
    +------+------------+
    | id   | ordertotal |
    +------+------------+
    |    1 |        100 |
    |    1 |         50 |
    +------+------------+
    2 rows in set (0.00 sec)mysql> select  t1.username, ifnull(sum(t2.ordertotal),0) as ordertotal from xiao
    shou t1  left  join  dingdan t2  on t1.id = t2.id  group by t1.username;
    +----------+------------+
    | username | ordertotal |
    +----------+------------+
    | lisi     |          0 |
    | zhangsan |        150 |
    +----------+------------+
    2 rows in set (0.00 sec)
      

  4.   

    ifnull是mySql的,换成isnull即可
      

  5.   

    楼上的都不行啊,必须要有where ordertotal>0这个限制条件
      

  6.   

    select username,sum(ordertotal) as ordertotal from xiaoshou left join dingdan t2  on t1.id = t2.id where ordertotal>0 or username='李四' group by username如果仅仅是要李四,直接这样写。如果是所有为0的都要出来的话
    估计要多家一个字段来区别了,我是想不到有什么方法了
      

  7.   

    没看懂什么意思啊,你都where ordertotal>0就算怎么整也弄不出来赵四
      

  8.   

    select a.id,isnull(b.o_t,0) as ordertotal
    from xiaoshu a left join
    (select id, sum(ordertotal) as o_t from dingdan where ordertotal>0 group by id )  b
    on a.id=b.id
      

  9.   

    报歉,上面写错一个字段
    select a.usernamer,isnull(b.o_t,0) as ordertotal
    from xiaoshu a left join
    (select id, sum(ordertotal) as o_t from dingdan where ordertotal>0 group by id )  b
    on a.id=b.id
      

  10.   

    同上回答
    select a.usernamer,isnull(b.o_t,0) as ordertotal
    from xiaoshu a left join
    (select id, sum(ordertotal) as o_t from dingdan where ordertotal>0 group by id )  b
    on a.id=b.id