sqlserver的case when 语句如何转 mysql 
如以下sqlserver
select ifnull(case when adid>1 then adid end),0) from advertisement不知道是否mysql有对应sql

解决方案 »

  1.   

    这两天正在被mysql玩,借这个帖子说些想法。
    俺发现mysql好像无法支持在遍历过程中对变量赋值,举个例子说:
    select
        @x=case when column1<10 then 10 else column1 end
    from
        table1
    但这样的语句在sqlserver中就能运行
    请大家谈谈想法
      

  2.   

    可直接使用:create table tab1(col1 int, col2 int);
    insert into tab1 values(1,1);
    insert into tab1 values(10,10);
    insert into tab1 values(100,100);
    insert into tab1 values(null,0);mysql> select ifnull(case when col1>1 then col1 end, 0), col2 from tab1;
    +-------------------------------------------+------+
    | ifnull(case when col1>1 then col1 end, 0) | col2 |
    +-------------------------------------------+------+
    |                                         0 |    1 |
    |                                        10 |   10 |
    |                                       100 |  100 |
    |                                         0 |    0 |
    +-------------------------------------------+------+
    4 rows in set (0.00 sec)
      

  3.   

    to 二楼,你给 case 取个别名能满足吗?mysql> select case when col1<10 then 10 else col1 end x, col1, col2 from tab1;
    +------+------+------+
    | x    | col1 | col2 |
    +------+------+------+
    |   10 |    1 |    1 |
    |   10 |   10 |   10 |
    |  100 |  100 |  100 |
    | NULL | NULL |    0 |
    +------+------+------+
    4 rows in set (0.00 sec)
      

  4.   

    其实俺的目的是要在遍历的时候给变量赋值,然后在后面用到,这个语句只是取了个别名。俺测试了很长时间,好像不能在遍历的时候赋值。
    不过还是谢谢wenzhulz(触丝)的想法。:)