mysql學習筆記2

2021-07-04 13:10:27 字數 2189 閱讀 8404

mysql常用函式

1 字串函式

concat(s1,s2...sn):把傳入的引數連線成乙個字串 select concat(baiduadress,":",address) from kd_order;

insert(str,x,y,newstr):將字串str從第x位置開始,y個字元長的子串替換為字串newstr select insert(baiduadress,3,100,"哈哈") from kd_order;

lower(str) upper(str) select lower(pk_order),upper(pk_order) from kd_order;

left(str,n) right(str,n)分別返回最左邊和最右邊的n個字元 select left(pk_order,10),right(pk_order,5) from kd_order;

lpad(str,n,pad) rpad(str,n,pad)分別用字串pad對str的最左邊和最右邊進行填充直到長度為n個字元長度 select lpad(pk_order,100,"1"),rpad

(pk_order,100,"2") from kd_order;

ltrim(str) rtrim(str) trim(str)

repeat(str,x)返回str重複x次select repeat(address,2) from kd_order;

replace(str,a,b)用字串b代替字串str中所有出現的字串a

strcmp(s1,s2)比較字串的ascii碼值得大小

substring(str,x,y)返回字串str從第x位置起的y個字元長度的子串

2 數值函式

abs(-8) 返回絕對值

ceil(x) 返回x的最小整數值

floor(x) 返回x的最大整數值

rand() 返回0到1之間的隨機數

mod(x,y)返回x/y的模

round(x,y)返回x四捨五入的有y位小數的值

truncate(x,y)返回x截斷為y位小數的結果

3 日期和時間函式

select current_timestamp();

select current_time();

select current_date();

select now();

select unix_timestamp(now());返回日期的unix時間戳

from_unixtime(unixtime) 和unix_timestamp(now())互為逆操作

select year(createtime),month(createtime),day(createtime),hour(createtime),minute(createtime),second(createtime),week

(createtime),monthname(createtime) from kd_order;

select date_format(createtime,'%y-%m-%d %h-%i-%s'),date_format(createtime,'%y-%m-%d %t') from kd_order;

select date_add(now(),interval 30 day),datediff('2016-09-09',now()) from kd_order;

4 流程函式

if(value,t,f)如果value為真返回t否則返回f select if(datediff('2016-09-09',now())=393,'0','1') from kd_order;

ifnull(value,t,f)如果value為null返回t否則返回f

case when x then r1 else r2 end

case y when x then r1 else r2 end :select case 393 when datediff('2016-09-09',now()) then 2 else 3 end from kd_order;

5 其他常用函式

database() 當前資料庫名

version() 資料庫版本

user() 登入使用者名稱

password(str) 返回str的加密版本

md5(str) 返回str的md5值

inet_aton(ip)返回ip位址的數字表示

inet_ntoa(num)返回數字代表的ip位址

mysql學習筆記(2)

mysql中資料型別 1.整形 整形大小 位元組 數值 有符號 tinyint 1 128 127 smallint 2 2 15 2 15 1 mediumint 3 2 23 2 23 1 int4 2 32 2 32 1 bigint 8 2 63 2 63 1 tinyint m unsig...

mysql學習筆記2

約束 作用 為了保證資料的有效性和完整性 mysql中常用的約束 主鍵約束 primary key 唯一約束 unique 非空約束 not null 外來鍵約束 foreign key 主鍵約束 被修飾過的字段唯一非空 注意 一張表只能有乙個主鍵,這個主鍵可以包含多個字段 方式1 建表的同時新增約...

mysql學習筆記(2)

mysql 函式 1.length 獲取引數值的位元組個數 select length join select length 漢字所佔位元組 2.concat 拼接字串 select concat last name,frist name 姓名 from table name 3.upper low...