sql mysql 常用函式總結

2021-09-02 23:06:38 字數 3449 閱讀 2605

//注意

mysql `` 的重要性 避免與mysql]中的關鍵字 相同而衝突

//型別

decimal(20,3) 型別 引數幾位數字 保留小數點後幾位

//時間 當天0點

curdate()

select date_sub(curdate(),interval 1 day)

select date_format(date_sub(curdate(),interval 1 day),'%y-%m-%d %h:%i:%s');// -1是明天

//count函式相關

count(1) and count(字段)

兩者的主要區別是

(1) count(1) 會統計表中的所有的記錄數,包含欄位為null 的記錄。

(2) count(字段) 會統計該字段在表中出現的次數,忽略欄位為null 的情況。即不統計欄位為null 的記錄。

執行效率上:  

列名為主鍵,count(列名)會比count(1)快  

列名不為主鍵,count(1)會比count(列名)快  

如果表多個列並且沒有主鍵,則 count(1) 的執行效率優於 count(*)  

如果有主鍵,則 select count(主鍵)的執行效率是最優的  

如果表只有乙個字段,則 select count(*)最優。

//轉型

//mysql 非空 非空字串 判斷

,if((isnull(aaa) || length(trim(aaa)) 1),'-',aaa)

//常用函式--

//uuid

1.uuid select replace(uuid(), '-','');//替換

//隨機數

2.select ceiling(rand()*9+1);ceiling(rand()*9000000000+1000000000);//在inset** select** 可以使用此生成不同id

//擷取第乙個,之前的位置

substring_index(***,',',1);

//拼接某引數  使用ifnull 更嚴謹

4.select concat('www',ifnull(e.mon_point_asset_no,''))

//時間-日期轉換

時間轉 字串 date_format(colum, '%y-%m-%d %h:%i:%s')

字串樁時間 str_to_date(colum,'%y-%m-%d %h:%i:%s')

//時間差

select timestampdiff(day, '2018-03-20 23:59:00', null);#返回null

select timestampdiff(hour, null, '2018-03-22 10:00:00');#返回null

select timestampdiff(minute, '2018-03-20 09:00:00', '2018-03-22 10:00:00');

//將 總秒值 轉為n天n時n分n秒

ifnull((timestampdiff(second,o.start_time,o.last_time)div (24 * 60 * 60)),0) as day,

ifnull (((timestampdiff(second ,o.start_time,o.last_time)mod(24*60*60)) div(60*60)),0) as hou,

ifnull (((timestampdiff(second ,o.start_time,o.last_time)mod(60*60)) div(60)),0) as min,

ifnull ((timestampdiff(second ,w.warn_begin_time, w.warn_end_time)mod(60)),0) as sed

//ifnull

8.ifnull(expr1,expr2)// 如果第乙個引數不為空,則返回第乙個引數,否則返回第二個引數。

//find_in_set 函式

select find_in_set('d','a,b,c,d'); #返回4

select find_in_set('6', '1'); #返回0

#總結:所以如果list是常量,則可以直接用in, 否則要用find_in_set()函式

select * from a_project where 'dd' in (字段);#不能查詢出值!

select * from a_project where 'dd' in ('cc','dd','ee','ff'); # in 必須一樣才能查詢出結果

#find_in_set 可以解決這個問題

select * from a_project where find_in_set('ee',字段);

#如果list是常量,則可以直接用in, 否則要用find_in_set()函式。

#ike是廣泛的模糊查詢,而 find_in_set() 是精確匹配

select * from a_project where find_in_set(p_id, '3654882128,9336264865');

//concat_ws ,group_concat函式 //可用於列轉行

select concat_ws('|-.-|',p_id,p_name,p_content ) from a_project ;

#查詢name 相同的人的所有id

select max(p_num) from a_project group by p_name;

#顯示相同名字的人的id號

select p_name, group_concat(p_id) from a_project group by p_name; #預設就是用','分開

#查詢name 相同的所有id 以',分隔,並將id的順序按照p_num 排序

select p_name, group_concat(p_id order by p_num desc separator '!-.-!') from a_project group by p_name;

在mysql中sum進行減法操作的時候:

sum(b.real_price) - sum(b.cost_price)

錯誤寫法:

//但是這種 寫法在oracle和db2中是可以的

sum(b.real_price-b.cost_price)

sql mysql 日期函式 今天昨天

mysql 今天和昨天日期及格式化 今天日期 時間 select now 返回2018 04 25 17 33 21 今天日期 select curdate 返回2018 04 25 今天日期格式化 select date format curdate y m d 返回2018 04 25 昨天日期...

常用函式總結

includeunsigned long int htonl unsigned long int hostlong unsigned short int htons unsigned short int hostshort unsigned long int ntohl unsigned long ...

常用函式總結

isset 檢查變數是否設定 unset 銷毀變數 var dump 列印變數的詳細資訊 echo 輸出乙個或多個字串 print 輸出字串 printf 輸出格式化字串 sprintf return a formatted string print r 列印關於變數的易於解釋的資訊 d efine...