mysql条件判断、临时变量

来源:互联网 发布:山西省软件行业协会 编辑:程序博客网 时间:2024/06/06 10:24

参考xujiali5172923的博客,网址:http://blog.csdn.net/xujiali5172923/article/details/50545398

1 条件判断

if(expr,V1,V2)  //如果expr为真,则返回V1,否则返回V2


mysql> select if(3>2,2,3);
+-------------+
| if(3>2,2,3) |
+-------------+
|           2 |
+-------------+
1 row in set


case 表达式(expr) when v1 then value1 when v2 then value2 else  value3;

当expr等于v1,v2或者其他时,分别取对应的值。


mysql> select case 2 when 1 then 'one' when2 then 'two' else 'more' end;
+------------------------------------------------------------+
| case 2 when 1 then 'one' when 2 then 'two' else 'more' end |
+------------------------------------------------------------+
| two                                                        |
+------------------------------------------------------------+
1 row in set



2  临时变量

mysql> SET @one := 1;

select @one;

SET @one := 2;

select @one;



Query OK, 0 rows affected

+------+
| @one |
+------+
|    1 |
+------+
1 row in set

Query OK, 0 rows affected

+------+
| @one |
+------+
|    2 |
+------+
1 row in set