Mysql IF相關函式的使用

2021-09-29 14:29:55 字數 1145 閱讀 2408

if函式可以在sql中使用 也可以在儲存過程中控制語句流程

在sql中if(expr1,expr2,expr3)如果expr1條件為true/不為0/不為null 返回expr2 否則 返回expr3;

select if(gender == 1, 『男』, 『女』) as gender from user;

同樣的功能 用case when也可以實現

select case gender when 1then '男』else 『女』 end as gender from user;

case expr1 when 1 then 『a』 when 2 then 『b』 else 『c』 end

在儲存過程中

if condition1 then

執行的語句1

elseif condition2 then

執行的語句2

else

執行的語句3

end if

ifnull(expr1,expr2)如果expr1不為空,返回expr1 為空則返回expr2

if(null,1) 返回1

if(1,2) 返回1

nullif(expr1,expr2)如果expr1=expr2 返回null 如果expr1 != expr2 返回expr1

nullif(『a』,『a』) 返回null

nullif(『a』,『b』) 返回a

寫到了null就把isnull()判斷也加上吧

isnull(expr1)expr1為空 返回1 不為空 返回0

isnull(null) 返回1

isnull(『a』) 返回0

elt(expr1,str1,str2,…)expr1=1 返回str1 expr1=2 返回str2 如果 expr1 小於 1 或大於引數的數量,返回 null

比如我們用在會員等級上

select elt(level, 『超級會員』, 『**會員』, 『普通會員』) as level_name from user;

mysql if集合 MySQL使用if判斷詳解

select if sva 1,男 女 as ssva from taname where sva 12.2.控制流程函式 case value when compare value then result when compare value then result else result end...

MATLAB函式的相關使用

function 輸出引數 函式名 輸入引數 函式體end 注意事項 m檔案中乙個函式為主函式,其餘函式皆為子函式,呼叫該檔案函式時,率先呼叫主函式 在乙個m檔案種,除主函式外的所有函式皆為子函式,子函式不能與主函式重名 函式體內定義其他函式,所有函式必須以end結尾 通過不同輸入輸出引數重寫函式,...

php array的相關函式使用

array change key case array array int case case lower array 兩個引數,第乙個陣列,第二個是常量 case lower case upper 預設是case lower 作用 鍵化大小寫 array chunk array array,int...