比如
score1, scroe2, score3 
--------------------- 
-9  -5  9 select   score1+scroe2+score3   结果是-5但是,我想让结果为负数的时候,抽取出来的数据为0,应该怎么做呢

解决方案 »

  1.   

    select case when score1+score2+score3<0 then 0 end as score from tb
      

  2.   

    select if((score1+scrore2+score3)<0,0,(score1+score2+score3)) as score from tb
      

  3.   

    select   if((score1+scrore2+score3) <0,0,(score1+score2+score3))   as   score   from   tb这个可以。
      

  4.   


    mysql> create table e(score1 int not null,score2 int not null,score3 int not nul
    l);
    Query OK, 0 rows affected (0.03 sec)mysql> insert into e values(-9,-5,9);
    Query OK, 1 row affected (0.03 sec)mysql> select * from e;
    +--------+--------+--------+
    | score1 | score2 | score3 |
    +--------+--------+--------+
    |     -9 |     -5 |      9 |
    +--------+--------+--------+
    1 row in set (0.02 sec)mysql> select (case when score1+score2+score3 < 0 then 0 else score1+score2+scor
    e3 end) as score from e;
    +-------+
    | score |
    +-------+
    |     0 |
    +-------+
    1 row in set (0.00 sec)
      

  5.   

    用case会简单很多呢,当然liangCK写得有点问题,没有处理score不为负的情况,正常的应该是:select case when score1+score2+score3<0 then 0 else score1+score2+score3 end as score from tb