MySQL函數

原文連接http://zhhll.icu/2020/12/28/%E6%95%B0%E6%8D%AE%E5%BA%93/%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/MySQL%E5%87%BD%E6%95%B0/mysql

MySQL函數

字符函數

length字節個數

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返回當前系統時間

select now();

curdate返回當前系統日期,不包含時間

select curdate();

curtime返回當前系統時間

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');

流程控制

流程控制結構分爲順序結構、分支結構、循環結構sql

順序結構

從上往下依次執行函數

分支結構

從兩條或多條分支選擇一條執行oop

if函數 if-else效果
#語法 if(表達式1,表達式2,表達式3)  表達式1成立,則執行表達式2,不然執行表達式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;

循環結構

在知足必定的條件下,重複執行一段代碼code

循環控制orm

  • iterate 相似於continue
  • leave 相似於break
while結構
【標籤:】while 循環條件 do
	循環體
end while 【標籤】;
loop結構
【標籤:】loop
	循環體
end loop 【標籤】;
repeat結構
【標籤:】 repeat 
	循環體
until 捷順循環的條件
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索引

相關文章
相關標籤/搜索