MySQL函數
字符函數
length字節(jié)個數
select length('john');
concat拼接字符串
select concat('I',' ','like',' ','mysql');
upper/lower大小寫轉換
select upper('mysql');
select lower('MySQL');
substr/substring字符串截取
#從第幾個字符開始截取,注意:索引從1開始
select substr('I like MySQL',8);
#從第幾個字符開始截取,截取幾個字符
select substr('I like MySQL',8,2);
instr子串在字符串中的起始索引
select instr('I like MySQL','MySQL');
trim去掉前后空格
select trim(' My SQL ');
lpad用指定字符進行左填充達到指定長度
select lpad('MySQL',8,'*');
rpad用指定字符進行右填充達到指定長度
select rpad('MySQL',8,'*');
replace替換
select replace('aabb','bb','cc');
數學函數
round四舍五入
select round(1.4);
#小數點后保留幾位
select round(1.567,2);
ceil向上取整
select ceil(1.2);
floor向下取整
select floor(1.2);
truncate截斷(小數點后保留幾位)
select truncate(1.61,2);
mod取模
select mod(10,3);
日期函數
now返回當前系統(tǒng)時間
select now();
curdate返回當前系統(tǒng)日期,不包含時間
select curdate();
curtime返回當前系統(tǒng)時間
select curtime();
獲取指定的部分
select year('2020-10-10');
select month('2020-10-10');
str_to_date 將日期格式的字符轉換成指定格式的日期
select str_to_date('2020年10月12','%Y年%m月%d');
date_format將日期轉換為字符
select date_format('2020/10/12','%Y-%m-%d');
流程控制
流程控制結構分為順序結構趟脂、分支結構各聘、循環(huán)結構
順序結構
從上往下依次執(zhí)行
分支結構
從兩條或多條分支選擇一條執(zhí)行
if函數 if-else效果
#語法 if(表達式1,表達式2,表達式3) 表達式1成立片效,則執(zhí)行表達式2禁筏,否則執(zhí)行表達式3
select if(6<3,'小于','大于');
case函數
相當于switch-case
#語法
case 表達式|變量|字段
when 要判斷的值 then 結果
when 要判斷的值 then 結果
...
else result
end
相當于多重if語句
#語法
case
when 要判斷的條件 then 結果
when 要判斷的條件 then 結果
...
else result
end
if結構
#語法
if 條件1 then 語句1;
elseif 條件2 then 語句2;
else 語句;
end if;
循環(huán)結構
在滿足一定的條件下荸型,重復執(zhí)行一段代碼
循環(huán)控制
- iterate 類似于continue
- leave 類似于break
while結構
【標簽:】while 循環(huán)條件 do
循環(huán)體
end while 【標簽】;
loop結構
【標簽:】loop
循環(huán)體
end loop 【標簽】;
repeat結構
【標簽:】 repeat
循環(huán)體
until 捷順循環(huán)的條件
end repeat 【標簽】;
聚合函數
求和
select sum(salary) from employees;
平均值
select avg(salary) from employees;
最大值
select max(salary) from employees;
最小值
select min(salary) from employees;
個數
select count(id) from users;
由于本身的博客百度沒有收錄繁涂,博客地址http://zhhll.icu