《Mysql應用mysql內置函數case用法介紹》要點:
本文介紹了Mysql應用mysql內置函數case用法介紹,希望對您有用。如果有疑問,可以聯系我們。
導讀:本節內容:mysql內置函數case使用介紹mysql對case函數的解釋:
mysql> ? caseMany help items for your request exist.To make a...
本節內容:
mysql內置函數case使用介紹MYSQL教程
mysql對case函數的解釋:
?MYSQL教程
mysql> ? case
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
?? CASE OPERATOR
?? CASE STATEMENT
mysql> ? case operator
Name: 'CASE OPERATOR'
Description:
Syntax:
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.
?
URL: http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.htmlMYSQL教程
例子:
?MYSQL教程
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
例1:
?MYSQL教程
mysql> SELECT CASE 1 WHEN 1 THEN 'one'
??? ->???? WHEN 2 THEN 'two' ELSE 'more' END;
??????? -> 'one'
?
如果case后面的表達式和when中的值相等,則返回相對應then后的值,否則返回else的值.MYSQL教程
例2:
?MYSQL教程
mysql> SELECT CASE BINARY 'B'
??? ->???? WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
??????? -> NULL
?
這個例子和上面的類似,只不過沒有else值,返回nullMYSQL教程
例3:
?MYSQL教程
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
??????? -> 'true'
?
如果case后面的值為真,返回then值,否則返回else值.MYSQL教程
案例:統計各班級中的及格與不及格人數
?MYSQL教程
mysql> select * from student;
+----+-------+-------+-------+
| id | class | name? | score |
+----+-------+-------+-------+
|? 1 |???? 1 | name1 |??? 50 |
|? 2 |???? 1 | name2 |??? 30 |
|? 3 |???? 2 | name1 |??? 60 |
|? 4 |???? 1 | name2 |??? 30 |
|? 5 |???? 2 | name1 |??? 60 |
|? 6 |???? 1 | name2 |??? 70 |
|? 7 |???? 2 | name1 |??? 60 |
|? 8 |???? 1 | name2 |??? 70 |
|? 9 |???? 2 | name1 |??? 60 |
| 10 |???? 3 | name2 |??? 70 |
| 11 |???? 2 | name1 |??? 60 |
| 12 |???? 3 | name2 |??? 20 |
| 13 |???? 2 | name1 |??? 60 |
| 14 |???? 3 | name2 |??? 20 |
+----+-------+-------+-------+
14 rows in set (0.00 sec)
SQL語句:
?MYSQL教程
mysql> select class,count(case when score>=60 then 1 end) as '及格人數',count(case when score<60 then 1 end) as '不及格人數',count(*) as '總人數' from student group by class;
+-------+--------------+-----------------+-----------+
| class | 及格人數???? | 不及格人數????? | 總人數??? |
+-------+--------------+-----------------+-----------+
|???? 1 |??????????? 2 |?????????????? 3 |???????? 5 |
|???? 2 |??????????? 6 |?????????????? 0 |???????? 6 |
|???? 3 |??????????? 1 |?????????????? 2 |???????? 3 |
+-------+--------------+-----------------+-----------+
3 rows in set (0.00 sec)
以上通過實例介紹了mysql內置函數case的用法,希望對大家有所贊助.MYSQL教程
歡迎參與《Mysql應用mysql內置函數case用法介紹》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.fzlkiss.com/jiaocheng/12028.html