MYSQL mysql中內建函式

2022-06-15 11:18:15 字數 2439 閱讀 1515

mysql中內建函式

mysql中內建函式

需要掌握函式:date_format 

#1 基本使用

mysql> select date_format('2009-10-04 22:23:00', '%w %m %y');

-> 'sunday october 2009'

mysql> select date_format('2007-10-04 22:23:00', '%h:%i:%s');

-> '22:23:00'

mysql> select date_format('1900-10-04 22:23:00',

-> '%d %y %a %d %m %b %j');

-> '4th 00 thu 04 10 oct 277'

mysql> select date_format('1997-10-04 22:23:00',

-> '%h %k %i %r %t %s %w');

-> '22 22 10 10:23:00 pm 22:23:00 00 6'

mysql> select date_format('1999-01-01', '%x %v');

-> '1998 52'

mysql> select date_format('2006-06-00', '%d');

-> '00'

#2 準備表和記錄

create table blog (

id int primary key auto_increment,

name char (32),

sub_time datetime

);insert into blog (name, sub_time)

values

#3. 提取sub_time欄位的值,按照格式後的結果即"年月"來分組

select date_format(sub_time,'%y-%m'),count(1) from blog group by date_format(sub_time,'%y-%m');

#結果+-------------------------------+----------+

| date_format(sub_time,'%y-%m') | count(1) |

+-------------------------------+----------+

| 2015-03 | 2 |

| 2016-07 | 4 |

| 2017-03 | 3 |

+-------------------------------+----------+

3 rows in set (0.00 sec)

自定義函式

ps:

函式中不要寫sql語句(否則會報錯),函式僅僅只是乙個功能,是乙個在sql中被應用的功能

若要想在begin...end...中寫sql,請用儲存過程

#1

delimiter //

create function f1(

i1 int,

i2 int)

returns int

begin

declare num int;

set num = i1 + i2;

return(num);

end //

delimiter ;

#2delimiter //

create function f5(

i int

)returns int

begin

declare res int default 0;

if i = 10 then

set res=100;

elseif i = 20 then

set res=200;

elseif i = 30 then

set res=300;

else

set res=400;

end if;

return res;

end //

delimiter ;

刪除函式

drop function func_name;
執行函式

# 獲取返回值

select upper('egon') into @res;

select @res;

# 在查詢中使用

select f1(11,nid) ,name from tb2;

python中內建函式

python中有很多內建的功能函式,選取幾個做為筆記記錄如下 abs abs 返回引數的絕對值 abs 1 1 abs 10.10.0 abs 1.2 2.1j 2.4186773244895647 abs 0.22 0.77 0.55 coerce coece 資料型別轉換函式,返回乙個包含型別轉...

建立函式mysql MySql建立函式

首先需要檢視一下建立函式的功能是否開啟 mysql show variables like func variable name value log bin trust function creators on 1 row in set 0.02 sec 如果value處值為off,則需將其開啟。m...

mysql mysql的replace函式很容易

前段時間把 換了個網域名稱,結果發現資料庫很多記錄裡面都含有之前 的 因為 用到的資料庫是mysql資料庫,所以用mysql的replace函式很容易就可以把原來的 都替換成新的 update table name set fielda replace fielda,要替換的 新 update go...