mysql> select mod(29,9); //取模函數 +-----------+ | mod(29,9) | +-----------+ | 2 | +-----------+ 1 row in set (0.00 sec) mysql> select mod(29,2); +-----------+ | mod(29,2) | +-----------+ | 1 | +-----------+ 1 row in set (0.00 sec) mysql> select 2 between 1 and 10; //between xx and xx +--------------------+ | 2 between 1 and 10 | +--------------------+ | 1 | +--------------------+ 1 row in set (0.06 sec) mysql> select 20 between 1 and 10; +---------------------+ | 20 between 1 and 10 | +---------------------+ | 0 | +---------------------+ 1 row in set (0.00 sec) mysql> select 20 not between 1 and 10; //not between xx and xx +-------------------------+ | 20 not between 1 and 10 | +-------------------------+ | 1 | +-------------------------+ 1 row in set (0.00 sec) mysql> select 20 not between 1 and 50; +-------------------------+ | 20 not between 1 and 50 | +-------------------------+ | 0 | +-------------------------+ 1 row in set (0.00 sec) mysql> select greatest(1,2,56,7,5); //greatest函數,在一串數字中取最大值 +----------------------+ | greatest(1,2,56,7,5) | +----------------------+ | 56 | +----------------------+ 1 row in set (0.08 sec) mysql> select greatest('a','b','c','d','e');//字母取最大值 +-------------------------------+ | greatest('a','b','c','d','e') | +-------------------------------+ | e | +-------------------------------+ 1 row in set (0.35 sec) ISNULL(expr) //是空值 如expr 為NULL,那么ISNULL() 的返回值為 1,否則返回值為 0 mysql> select isnull(5); //是空值,為真則返回1,否則返回0 +-----------+ | isnull(5) | +-----------+ | 0 | +-----------+ 1 row in set (0.00 sec) mysql> select isnull(null);; +--------------+ | isnull(null) | +--------------+ | 1 | +--------------+ 1 row in set (0.00 sec) LEAST(value1,value2,...) 在有兩個或多個參數的情況下, 返回值為最小 (最小值) 參數 mysql> select least(2,0,1,-9,5,4); +---------------------+ | least(2,0,1,-9,5,4) | +---------------------+ | -9 | +---------------------+ 1 row in set (0.02 sec) mysql> select least('a','b','c','d','e'); +----------------------------+ | least('a','b','c','d','e') | +----------------------------+ | a | +----------------------------+ 1 row in set (0.00 sec) mysql> select (case 1 when 1 then 'one' //case語法 -> when 2 then 'two' -> else 'more' -> end) as cid; +-----+ | cid | +-----+ | one | +-----+ 1 row in set (0.00 sec) //案例解析 select 'AAA', //標記1 (case cid when '3' then 'xxxx' when '4' then 'xxxx' when '5' then 'xxxx' else cid end ) as 渠道名稱, //標記2 intdate as 注冊日期, from 表名 where intdate>= '20161020' and intdate<= '20161103' //解析 1)標記2為一個語句 2)as將語句重命名為渠道名稱 3)case語法結構:(case cid when '1' then 'xxxx' when '2' then 'xxxx' else cid end ) ,從cid中匹配到編號1的時候,返回的結果將1賦值為xxxx 4)else cid,當cid不為1,2時,將直接返回cid本身 //if函數 //IF(expr1,expr2,expr3) 如果 expr1 是TRUE,則 IF()的返回值為expr2; 否則返回值則為 expr3。 IF() 的返回值為數字值或字符串值,具體情況視其所在語境而定 mysql> select if(1>5,'yes','no'); //和excel中的if函數用法一致 +--------------------+ | if(1>5,'yes','no') | +--------------------+ | no | +--------------------+ 1 row in set (0.00 sec) mysql> select if(1<5,'yes','no'); +--------------------+ | if(1<5,'yes','no') | +--------------------+ | yes | +--------------------+ 1 row in set (0.00 sec) //CONCAT(str1,str2,...) mysql> select concat('my','sql'); //mysql +--------------------+ | concat('my','sql') | +--------------------+ | mysql | +--------------------+ 1 row in set (0.38 sec) mysql> select concat('my','null','sql'); //mynullsql +---------------------------+ | concat('my','null','sql') | +---------------------------+ | mynullsql | +---------------------------+ 1 row in set (0.00 sec) mysql> select concat('my',null,'sql'); //NULL +-------------------------+ | concat('my',null,'sql') | +-------------------------+ | NULL | +-------------------------+ 1 row in set (0.00 sec) mysql> select concat(14.3); //14.3 +--------------+ | concat(14.3) | +--------------+ | 14.3 | +--------------+ 1 row in set (0.00 sec) mysql> select concat(14.3,25); //14.325 +-----------------+ | concat(14.3,25) | +-----------------+ | 14.325 | +-----------------+ 1 row in set (0.00 sec) //INSTR(str,substr) 返回字符串 str 中子字符串的第一個出現位置。這和LOCATE()的雙參數形式相同,除非參數的順序被顛倒 mysql> select instr('foobarbar','bar'); +--------------------------+ | instr('foobarbar','bar') | +--------------------------+ | 4 | +--------------------------+ 1 row in set (0.35 sec) mysql> select lower('MySQL'); //lower和lcase轉化為小寫 +----------------+ | lower('MySQL') | +----------------+ | mysql | +----------------+ 1 row in set (0.00 sec) mysql> select lcase('MySQL'); +----------------+ | lcase('MySQL') | +----------------+ | mysql | +----------------+ 1 row in set (0.00 sec) mysql> select left('foobar',4); //從左向右取數據,取4個數據 +------------------+ | left('foobar',4) | +------------------+ | foob | +------------------+ 1 row in set (0.00 sec) mysql> select right('foobar',4); //從右向左取數據,取4個數據 +-------------------+ | right('foobar',4) | +-------------------+ | obar | +-------------------+ 1 row in set (0.36 sec) mysql> select length('mysql'); //length求字符串的長度 +-----------------+ | length('mysql') | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) //返回字符串 str ,其引導空格字符被刪除 mysql> select ltrim(' bar') as str; //ltrim刪除左邊的空格引導字符 +------+ | str | +------+ | bar | +------+ 1 row in set (0.00 sec) mysql> select rtrim(' bar ') as str; //rtrim刪除右邊的空格引導字符 +-------+ | str | +-------+ | bar | +-------+ 1 row in set (0.05 sec) mysql> select trim(' bar ') as str; //trim刪除2邊的空格引導符 +------+ | str | +------+ | bar | +------+ 1 row in set (0.00 sec) //SUBSTRING substring(str, pos); substring(str, pos, len) 從字符串的第pos個字符位置開始取,取len個數據,直到結束。 mysql> select substring('example',4,2); +--------------------------+ | substring('example',4,2) | +--------------------------+ | mp | +--------------------------+ 1 row in set (0.00 sec)
