mysql使用判斷 MySQL使用判斷

2021-10-17 13:07:22 字數 941 閱讀 8112

1.case語法

在第乙個方案的返回結果中, value=compare-value。而第二個方案的返回結果是第一種情況的真實結果。如果沒有匹配的結果值,則返回結果為else後的結果,如果沒有else 部分,則返回值為 null。

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

2.if語法

if(expr1,expr2,expr3)

如果 expr1 是true (expr1 <> 0 and expr1 <> null),則 if()的返回值為expr2; 否則返回值則為 expr3。if() 的返回值為數字值或字串值,具體情況視其所在語境而定。

mysql> select if(1>2,2,3);

-> 3

mysql> select if(1<2,yes ,no);

->yes

-- strcmp 比較兩字串

mysql> select if(strcmp(test,test1),no,yes);

-> no

如果expr2 或expr3中只有乙個明確是 null,則if() 函式的結果型別 為非null表示式的結果型別。

expr1 作為乙個值進行計算時,非null為true。

mysql> select if(null,1,0);

-> 0

mysql> select if(0.1<>0,1,0);

-> 1

mysql語句使用 MySQL 基本語句的使用

1.create 建立命令 建立新資料庫 mydb create database mydb 選擇資料庫 mydb use mydb 在當前資料庫中建立資料表 table1 create table if not exists table1 fileid int not null auto incr...

mysql如何使用索引 MySQL是如何使用索引的

閒扯 很多時候我們面對很慢的查詢的時候會一籌莫展,這個時候大部分人都會很自然的想到建索引這條路。事實上索引確實是個很好的優化方式,乙個良好的索引能夠提公升不止一倍的效率,還能帶來併發能力的提公升。但是索引也不是萬能的,不然的話我大可以給一張表的所有列上都加上索引,但是基本上所有的dba都會有一條認知...

mysql游標使用範圍 MySql游標的使用例項

mysql游標使用的整個過程為 1.建立游標 複製 如下 declare calc bonus cursor for select id,salary,commission from employees 2.開啟游標 複製 如下 open calc bonus 3.使用游標 複製 如下 fetch ...