04 MySQL常見函式 單行函式

2022-08-19 02:30:11 字數 4900 閱讀 7558

#單行函式細分

1、字元函式

2、數學函式

3、日期函式

4、其他函式

5、流程控制函式

#單行函式 - 字元函式

#一、字元函式

#1. length 獲取引數的位元組長度

select length('john');

select length('張三丰'); #utf-8編碼,1個漢字3個位元組

# 檢視當前客戶端的字符集

show variables like '%char%';

#2. concat 拼接字元

select concat(last_name,'_',first_name) from employees;

#3. upper,lower

select upper('john');

select lower('john');

#案例:將姓變大寫,將名變小寫,然後拼接

select concat(upper(first_name), lower(first_name)) as "姓名" from employees;

#4. substr, substring(同乙個函式)

#4個過載的方法

# 注意:sql語言的索引從1開始

# 作用:擷取從pos開始的所有字元,包括pos

select substr('zhangjin', 6); # jin

# 作用:擷取從pos開始, 字元長度為len的子串

select substr('zhangjin',1,5); # zhang

# 案例:姓名中首字母大寫,其他字元小寫

select concat(upper(substr(last_name,1,1)), lower(substr(last_name,2)))

as "姓"

from employees;

#5. instr 返回子串第一次出現的起始索引,沒有出現則返回0

select instr('zhangjin','shay'); # 0

select instr('zhangjin','jin'); # 6

select instr('zhangjinjinjinjinjinjin','jin'); # 6

#6、trim

# 預設是trim左右兩邊的空格

select trim(' zhangjin ');

# 可以指定要trim的字元

select trim('a' from 'aaaaaazhangjinaaaa');

#7、lpad

#使用指定字元進行左填充,填充後的字元個數為len

#len,字元個數,並不是位元組長度

select lpad('張',3,'*'); # **張

#最終長度由len決定

select lpad('張三丰',2,'*'); # 張三

#8、rpad

#使用指定字元進行右填充,填充後的字元個數為len

#len,字元個數,並不是位元組長度

select rpad('張',3,'*'); # 張**

#最終長度由len決定

select rpad('張三丰',2,'*'); # 張三

#9、replace

# replace(str,from_str,to_str) 全部替換,replace_all

select replace('zhangjin', 'zhang', 'shay');

select replace('zhangjinzhangjinzhangjin', 'zhang', 'shay');

#二、數學函式

#round 四捨五入

select round(1.45);

select round(-1.65);

select round(1.567,2); #保留2位小數

#ceil 向上取整, 返回大於等於該引數的最小整數

select ceil(1.002); #2

select ceil(1.00); #1

#floor 向下取整,返回小於等於該引數的最大整數

select floor(1.002); #1

#truncate 截斷

select truncate(1.65,1); #保留1位小數

#mod 取餘數

select mod(10,3); # 10%3=1

select mod(-10,-3); # -1, 被除數為負數,結果為負數

#三、日期函式

#now 返回當前系統日期+時間

select now();

#curdate 返回當前系統日期,不包括日期

select curdate();

#curtime 返回當前時間,不包括日期

select curtime();

#可以獲取指定的部分:年,月,日,時,分,秒

# 獲取年

select year(now()) as 年;

select year('1986-1-1');

select year(hiredate) from employees;

# 獲取月

select month(now()) as 月; # 11

select monthname(now()) as 月; # november

# 獲取日

select day(now()) as 日; # 24

select dayname(now()) as 日; # friday

s# 將日期格式的字串 -> 日期

# str_to_date('24-11-2017', '%d-%m-%y');

select str_to_date('24-11-2017', '%d-%m-%y');

# 案例:將使用者輸入的4-3 1992作為查詢依據,找出在2023年4月3日入職的員工

select * from employees where hiredate = str_to_date('4-3 1992', '%c-%d %y');

# 日期 -> 字元

# date_format('2018/6/6', '%y年%m月%d日')

select date_format('2018/6/6', '%y年%m月%d日');

# 案例:查詢有獎金的員工名和入職日期(xx月/xx日 xx年)

select last_name,date_format(hiredate, '%m月/%d日 %y年') as 入職日期

from employees

where commission_pct is not null;

#四、其他函式

# mysql版本查詢

select version();

# 當前使用的資料庫查詢

select database();

# 查詢當前使用者

select user(); #root@localhost

#五、流程控制函式

#1、if函式

# if(exp1,exp2,exp3) exp1為true, 返回exp2的值,否則返回exp3的值

select last_name, if(commission_pct is null, '無', '有') from employees;

#2、case控制結構的使用一:switch case的效果

/*適合:等值判斷

mysql中case控制結構:

case 要判斷的字段或表示式

when 常量1 then 要顯示的值1或語句1;

when 常量2 then 要顯示的值2或語句2;

when 常量3 then 要顯示的值3或語句3;

when 常量4 then 要顯示的值4或語句4;

else 要顯示的值n或語句n;

end*/

/*案例:查詢員工的工資,要求

部門號=30,顯示的工資為1.1倍

部門號=40,顯示的工資為1.2倍

部門號=50,顯示的工資為1.3倍

其他部門,顯示工資為原工資

*/select salary,department_id,

case department_id

when 30 then 1.1*salary

when 40 then 1.2*salary

when 50 then 1.3*salary

else salary

end

as "新工資"

from employees;

#3、case控制結構的使用二:類似多重if

/*mysql中case控制結構:

適合:區間判斷(大小)

case

when 條件1(true,false) 要顯示的值1(尾部不加分號)或語句1(尾部加分號)

when 條件2(true,false) 要顯示的值2(尾部不加分號)或語句2(尾部加分號)

....

else 要顯示的值n(尾部不加分號)或語句n(尾部加分號)

end*/

#案例:查詢員工的工資情況

/*如果工資大於2萬,顯示a級別

如果工資大於1萬5,顯示b級別

如果工資大於1萬,顯示c級別

否則,顯示d級別

*/select salary,

case

when salary>20000 then 'a'

when salary>15000 then 'b'

when salary>10000 then 'c'

else 'd'

end

as "工資級別"

from employees;

MySQL 常見函式(單行函式)

二,數學函式 三,日期函式 四,其他函式 五,流程控制函式 select 函式名 實參列表 from 表 1,單行函式 如concat,length,ifnull等 2,分組函式 又稱為統計函式,聚合函式,組函式 功能 做統計使用一,字元函式 1,length 函式 int length strin...

04 Mysql 初識sql語句

create database db1 charset utf8 檢視當前建立的資料庫 show create database db1 檢視所有的資料庫 show databases alter database db1 charset gbk drop database db1 use db1 ...

04 常見函式

字元函式 函式結果 大小寫控制函式 lower sql course sql course 大小寫控制函式 upper sql course sql course 字元控制函式 concat hello world helloworld 字元控制函式 substr helloworld 1,5 he...