table
id score
1 23
2 33table2
id score
4 22
6 45
SELECT A.id,B.id,SUM(A.score+B.score) FROM table A LEFT JOIN table B ON A.id=B.id我们知道,此left join并未查到东西。但是
id SUM()
NULL NULL
我的意思是不想显示null.应该是空值才是我要的。
如何办啊?试了以下方法,未成功。
where id>0
where id is not null
sum(COALESCE(A.score,0)+COALESCE(B.score,0))怎么才能实现,空集?

解决方案 »

  1.   

    SELECT ifnull(a.id,'') a_id,ifnull(b.id,'') b_id,ifnull(SUM(b.score+a.score),'') sum_score FROM  a LEFT JOIN  b ON a.id=b.id;
      

  2.   

    不要用统计函数SUM,用"+"操作就行了。mysql> select t1.id, ifnull(t2.id, '') t2_id, ifnull(t1.score + t2.score, '') score_sum  from t1 left join t2 on t1.id=t2.id;
    +------+-------+-----------+
    | id   | t2_id | score_sum |
    +------+-------+-----------+
    |    1 |       |           |
    |    2 |       |           |
    +------+-------+-----------+
    2 rows in set (0.00 sec)
    SUM函数经常用于多行累加的.
      

  3.   

    SELECT A.id,B.id,SUM(A.score)+SUM(B.score) FROM table A LEFT JOIN table B ON A.id=B.id
      

  4.   

    SELECT A.id,ifnull(B.id),ifnull((suM(A.score+B.score),0) as  A 
    LEFT JOIN table B 
    ON A.id=B.id;
      

  5.   

    SELECT A.id,ifnull(B.id,0),ifnull((suM(A.score+B.score),0) as  A 
    LEFT JOIN table B 
    ON A.id=B.id;