日期:2014-05-16  浏览次数:20637 次

请给出一个mysql下,使用,csae的例子,及详细用法。谢谢
我知道在orcale下可,能使用case when
但,我从未见到在mysql下使用。

在mysql下能用吗?
如何使用?
请明之。

------解决方案--------------------
可以使用。

你可以参考MYSQL官方免费手册中的例子和语法。

在查询语句中的函数式用法
引用
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END

CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END

The first version returns the result where value=compare_value. The second version returns the result for the first condition that is true. If there was no matching result value, the result after ELSE is returned, or NULL if there is no ELSE part.

mysql> SELECT CASE 1 WHEN 1 THEN 'one'
-> WHEN 2 THEN 'two' ELSE 'more' END;
-> 'one'
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
-> 'true'
mysql> SELECT CASE BINARY 'B'
-> WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
-> NULL



------解决方案--------------------
SQL code
select name,case when sex='0' then '男' else '女' end as sex,age
from tb