mysql函式整理

2021-10-06 13:13:52 字數 3828 閱讀 3059

一.字串函式

1.拼接字串concat(str1,str2...)

select concat('hello',',','word','!'); 

2.包含字元個數length(str)

select length('hello,word!');

3.擷取字串

--left(str,len)返回字串str的左端len個字元

--right(str,len)返回字串str的右端len個字元

--substring(str,pos,len)返回字串str的位置pos起len個字元

select substring('hello,word!',7,4);

4.去除空格

--ltrim(str)返回刪除了左空格的字串str

--rtrim(str)返回刪除了右空格的字串str

select rtrim('hello ');

5.大小寫轉換

--lower(str) 轉化為小寫

--upper(str) 轉化為大寫

select lower('hello');

二.數學函式

1.求四捨五入值round(n,d),n表示原數,d表示小數字置,預設為0

select round(1.8);

2.求x的y次冪pow(x,y)

select pow(2,3);

3.獲取圓周率pi()

select pi();

4.隨機數rand(),值為0-1.0的浮點數

select rand();

三.日期時間函式

1.當前日期current_date()

select current_date();

2.當前時間current_time()

select current_time();

3.當前日期時間now()

select now();

4.日期格式化date_format(date,format)

引數format:

%y 獲取年,返回完整年份

%y 獲取年,返回簡寫年份

%m 獲取月,返回月份

%d 獲取日,返回天值

%h 獲取時,返回24進製的小時數

%h 獲取時,返回12進製的小時數

%i 獲取分,返回分鐘數

%s 獲取秒,返回秒數

select date_format('2020-12-12 12:12:12','%y年%m月%d日 %h時%i分%s秒');

5.選取日期時間的各個部分:日期、時間、年、季度、月、日、小時、分鐘、秒、微秒

select date('2020-12-12 12:12:12');

select time('2020-12-12 12:12:12');

select year('2020-12-12 12:12:12');

select quarter('2020-12-12 12:12:12');

select month('2020-12-12 12:12:12');

select week('2020-12-12 12:12:12');

select day('2020-12-12 12:12:12');

select hour('2020-12-12 12:12:12');

select minute('2020-12-12 12:12:12');

select second('2020-12-12 12:12:12');

select microsecond('2020-12-12 12:12:12');

6.返回月份中的最後一天last_day()

select last_day('2020-02-01');

當前月份中有多少天

select now(), day(last_day(now())) as days;

7.日期時間計算date_add()

select date_add(now(), interval 1 day);

select date_add(now(), interval 1 hour);

select date_add(now(), interval 1 minute);

select date_add(now(), interval 1 second);

select date_add(now(), interval 1 microsecond);

select date_add(now(), interval 1 week);

select date_add(now(), interval 1 month);

select date_add(now(), interval 1 quarter);

select date_add(now(), interval 1 year);

8.日期、時間相減:datediff(date1,date2), timediff(time1,time2)

--datediff(date1,date2):兩個日期相減 date1 - date2,返回天數。

select datediff('2020-08-01', '2020-08-08'); -- -7

--timediff(time1,time2):兩個日期相減 time1 - time2,返回 time 差值。

select timediff('08:08:08', '00:00:00'); -- 08:08:08

9.字串轉換為日期str_to_date(str, format)

select str_to_date('08.09.2020 08:09:30', '%m.%d.%y %h:%i:%s'); --2020-08-09 08:09:30

10.日期,時間轉換成字串date_format(),time_format()

select date_format('2020-08-08 22:23:01', '%y-%m-%d %h:%i:%s');

四.流程控制

case語法:等值判斷,當值等於某個比較值的時候,對應的結果會被返回;如果所有的比較值都不相等則返回else的結果;如果沒有else並且所有比較值都不相等則返回null

case 值 when 比較值1 then 結果1 when 比較值2 then 結果2 ... else 結果 end

select case 1 when 1 then '男' when 2 then '女' else '其它' end as result;

五.自定義函式

1.語法

delimiter $$

create function 函式名稱(引數列表) returns 返回型別

begin

sql語句

end$$

delimiter ;

說明:delimiter用於設定分割符,預設為分號

在「sql語句」部分編寫的語句需要以分號結尾,此時回車會直接執行,所以要建立儲存過程前需

要指定其它符號作為分割符,此處使用//,也可以使用其它字元

2.建立函式my_trim,用於刪除字串左右兩側的空格:

delimiter $$

create function my_trim(str varchar(100)) returns varchar(100)

begin

return ltrim(rtrim(str));

end$$

delimiter ;

3.使用自定義函式

select my_trim('  hello ');

MySql函式整理

1 round x,d 四捨五入。round x 其實就是round x,0 也就是預設d為0 selectround 110.35,1 110.4 2 truncate x,d 直接擷取需要保留的小數字 selecttruncate 110.35,1 110.3 3 format x,d 四捨五入...

mysql之函式 整理

新建 createfunction function name 引數列表 returns返回值型別 函式體乙個函式應該屬於某個資料庫,可以使用db name.funciton name的形式執行當前函式所屬資料庫,否則為當前資料庫。多條語句應該使用begin end語句塊包含。注意,一定要有retu...

Mysql常用函式整理

1.字串函式 length 資料 字串位元組數 char length 資料 字串長度 left 資料,x 字串左邊的x個字元 right 資料,x 字串右邊的x個字元 concat 資料1,資料2,資料3.拼接字串 strcmp 資料1,資料2 比較字串 前者大於後者為1,等於為0,小於為 1 s...