《MYSQL數據庫mysql內置函數if用法介紹》要點:
本文介紹了MYSQL數據庫mysql內置函數if用法介紹,希望對您有用。如果有疑問,可以聯系我們。
導讀:本節內容:mysql內置函數if使用介紹查看mysql對if函數的解釋:
mysql> ? if functionName: IF FUNCTIONDescription:Syntax:IF(expr1,ex...
本節內容:
mysql內置函數if使用介紹MYSQL數據庫
查看mysql對if函數的解釋:
?MYSQL數據庫
mysql> ? if function
Name: 'IF FUNCTION'
Description:
Syntax:
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns
expr2; otherwise it returns expr3. IF() returns a numeric or string
value, depending on the context in which it is used.
?
URL: http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html
例子:
?MYSQL數據庫
mysql> SELECT IF(1>2,2,3);
??????? -> 3
mysql> SELECT IF(1<2,'yes','no');
??????? -> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
??????? -> 'no'
?
如果表達式1為TRUE,則返回表達式2的值,否則返回表達式3的值.MYSQL數據庫
例子:一張學生表,表中有字段id、name、sex,分別表示ID,學生姓名、性別代號(0為女,1為男)
?MYSQL數據庫
mysql> select * from student;
+----+-------+-----+
| id | name? | sex |
+----+-------+-----+
|? 1 | name1 |?? 0 |
|? 2 | name2 |?? 0 |
|? 3 | name3 |?? 1 |
|? 4 | name4 |?? 0 |
+----+-------+-----+
4 rows in set (0.00 sec)
?
查出學生的相關信息:
?MYSQL數據庫
mysql> select name,if(sex=0,'女','男') as sex from student;
+-------+-----+
| name? | sex |
+-------+-----+
| name1 | 女? |
| name2 | 女? |
| name3 | 男? |
| name4 | 女? |
+-------+-----+
4 rows in set (0.00 sec)
通過以上SQL語句可以直接獲取學生的性別信息,而不必要在PHP端再次進行判斷得到性別信息.MYSQL數據庫
歡迎參與《MYSQL數據庫mysql內置函數if用法介紹》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/12040.html