? function 函數
函數的作用比較大,一般多用在select查詢語句和where條件語句之后。按照函數返回的結果,
可以分為:多行函數和單行函數;所謂的單行函數就是將每條數據進行獨立的計算,然后每條數據得到一條結果。
如:字符串函數;而多行函數,就是多條記錄同時計算,得到最終只有一條結果記錄。如:
sum
、avg等
多行函數也稱為聚集函數、分組函數,主要用于完成一些統計功能。MySQL的單行函數有如下特征:
單行函數的參數可以是變量、常量或數據列。單行函數可以接受多個參數,但返回一個值。
單行函數就是它會對每一行單獨起作用,每一行(可能包含多個參數)返回一個結果。
單行函數可以改變參數的數據類型。單行函數支持嵌套使用:內層函數的返回值是外層函數的參數。
?
單行函數可以分為:
類型轉換函數;
位函數;
流程控制語句;
加密解密函數;
信息函數
單行函數
?
1、 char_length字符長度
select char_length (tel) from user ;
?
2、 sin函數
select sin(age) from user ;
select
sin(1.57);
?
3、 添加日期函數
select date_add( '2010-06-21' , interval 2 month );
interval是一個關鍵字,2 month是2個月的意思,2是數值,month是單位
select addDate( '2011-05-28' , 2);
在前面的日期上加上后面的天數
?
4、 獲取當前系統時間、日期
select
curdate();
select
curtime();
?
5、 加密函數
select md5( 'zhangsan' );
?
6、
Null
處理函數
select ifnull(birthday, 'is null birthday' ) from user ;
如果birthday為null,就返回后面的字符串
?
select nullif (age, 245) from user ;
如果age等于245就返回null,不等就返回age
?
select isnull(birthday) from user ;
判斷birthday是否為null
?
select if (isnull(birthday), 'birthday is null' , 'birthday not is null' ) from user ;
如果birthday為null或是0就返回birthday is null ,否則就返回birthday not is null ;類似于三目運算符
?
7、
case
流程函數
case函數是一個流程控制函數,可以接受多個參數,但最終只會返回一個結果。
select
name,
age,
(
case
sex
when 1 then '男'
when 0 then '女'
else '火星人'
end
) sex
from user ;
?
組函數
組函數就是多行函數,組函數是完成一行或多行結果集的運算,最后返回一個結果,而不是每條記錄返回一個結果。
1、 avg平均值運算
select avg (age) from user ;
select avg ( distinct age) from user ;
?
2、
count
記錄條數統計
select count (*), count (age), count ( distinct age) from user ;
?
3、
max
最大值
select max (age), max ( distinct age) from user ;
?
4、
min
最小值
select min (age), min ( distinct age) from user ;
?
5、
sum
求和、聚和
select sum (age), sum ( distinct age) from user ;
select sum (ifnull(age, 0)) from user ;
?
6、 group by 分組
select count (*), sex from user group by sex;
select count (*) from user group by age;
select * from user group by sex, age;
?
7、 having進行條件過濾
不能在where子句中過濾組,where子句僅用于過濾行。過濾group by需要having
不能在where子句中用組函數,having中才能用組函數
select count (*) from user group by sex having sex <> 2;
?
? 多表查詢和子查詢
數據庫的查詢功能最為豐富,很多時候需要用到查詢完成一些事物,而且不是單純的對一個表進行操作。而是對多個表進行聯合查詢,
MySQL中多表連接查詢有兩種規范,較早的SQL92規范支持,如下幾種表連接查詢:
等值連接
非等值連接
外連接
廣義笛卡爾積
SQL99規則提供了可讀性更好的多表連接語法,并提供了更多類型的連接查詢,SQL99支持如下幾種多表連接查詢:
交叉連接
自然連接
使用using子句的連接
使用on子句連接
全部連接或者左右外連接
?
SQL92的連接查詢
SQL92的連接查詢語法比較簡單,多將多個table放置在from關鍵字之后,多個table用“,”隔開;
連接的條件放在where條件之后,與查詢條件直接用and邏輯運算符進行連接。如果條件中使用的是相等,
則稱為等值連接,相反則稱為非等值,如果沒有任何條件則稱為廣義笛卡爾積。
廣義笛卡爾積: select s.*, c.* from student s, classes c;
等值: select s.*, c.* from student s, classes c where s.cid = c.id;
非等值: select s.*, c.* from student s, classes c where s.cid <> c.id;
select s.*, c.name classes from classes c, student s where c.id = s.classes_id and s.name is not null ;
?
SQL99連接查詢
1、交叉連接cross
join
,類似于SQL92的笛卡爾積查詢,無需條件。如:
select s.*, c.name from student s cross join classes c;
?
2、自然連接
natural
join查詢,無需條件,默認條件是將2個table中的相同字段作為連接條件,如果沒有相同字段,查詢的結果就是空。
select s.*, c.name from student s natural join classes c;
?
3、using子句連接查詢:using的子句可以是一列或多列,顯示的指定兩個表中同名列作為連接條件。
如果用natural join的連接查詢,會把所有的相同字段作為連接查詢。而using可以指定相同列及個數。
select s.*, c.name from student s join classes c using (id);
?
4、
join
… on連接查詢,查詢條件在on中完成,每個on語句只能指定一個條件。
select s.*, c.name from student s join classes c on s.classes_id = c.id;
?
5、 左右外連接:3種外連接, left [ outer ] join 、 right [ outer ] join ,連接條件都是通過用on子句來指定,條件可以等值、非等值。
select s.*, c.name from student s left join classes c on s.classes_id = c.id;
select s.*, c.name from student s right join classes c on s.classes_id = c.id;
?
子查詢
子查詢就是指在查詢語句中嵌套另一個查詢,子查詢可以支持多層嵌套。子查詢可以出現在2個位置:
from關鍵字之后,被當做一個表來進行查詢,這種用法被稱為行內視圖,因為該子查詢的實質就是一個臨時視圖
出現在where條件之后作為過濾條件的值
?
子查詢注意點:
子查詢用括號括起來,特別情況下需要起一個臨時名稱
子查詢當做臨時表時(在from之后的子查詢),可以為該子查詢起別名,尤其是要作為前綴來限定數據列名時
子查詢用作過濾條件時,將子查詢放在比較運算符的右邊,提供可讀性
子查詢作為過濾條件時,單行子查詢使用單行運算符,多行子查詢用多行運算符
?
將from后面的子查詢當做一個table來用:
select * from ( select id, name from classes) s where s.id in (1, 2);
當做條件來用:
select * from student s where s.classes_id in ( select id from classes);
select * from student s where s.classes_id = any ( select id from classes);
select * from student s where s.classes_id > any ( select id from classes);
? 操作符和函數
1、 boolean只判斷
select 1 is true , 0 is false , null is unknown ;
select 1 is not unknown , 0 is not unknown , null is not unknown ;
?
2、 coalesce函數,返回第一個非null的值
select coalesce ( null , 1);
select coalesce (1, 1);
select coalesce ( null , 1);
select coalesce ( null , null );
?
3、 當有2個或多個參數時,返回最大的那個參數值
select
greatest(2, 3);
select
greatest(2, 3, 1, 9, 55, 23);
select greatest( 'D' , 'A' , 'B' );
?
4、 Least函數,返回最小值,如果有null就返回null值
select
least(2, 0);
select least(2, 0, null );
select
least(2, 10, 22.2, 35.1, 1.1);
?
5、 控制流函數
select case 1 when 1 then 'is 1' when 2 then 'is 2' else 'none' end ;
select case when 1 > 2 then 'yes' else 'no' end ;
?
6、 ascii字符串函數
select ascii( 'A' );
select ascii( '1' );
?
7、 二進制函數
select
bin(22);
?
8、 返回二進制字符串長度
select bit_length (11);
?
9、 char將值轉換成字符,小數取整四舍五入
select char (65);
select char (65.4);
select char (65.5);
select char (65.6);
select char (65, 66, 67.4, 68.5, 69.6, '55.5' , '97.3' );
?
10、 using改變字符集
select charset( char (0*65)), charset( char (0*65 using utf8));
?
11、 得到字符長度char_length,
character_length
select char_length ( 'abc' );
select character_length ( 'eft' );
?
12、 compress壓縮字符串、uncompress解壓縮
select compress( 'abcedf' );
select uncompress(compress( 'abcedf' ));
?
13、 concat_ws分隔字符串
select concat_ws( '#' , 'first' , 'second' , 'last' );
select concat_ws( '#' , 'first' , 'second' , null , 'last' );
? 事務處理
動作
開始事務: start transaction
提交事務:
commit
回滾事務:
rollback
設置自動提交:
set
autocommit 1 | 0
atuoCommit系統默認是1立即提交模式;如果要手動控制事務,需要設置set autoCommit 0;
這樣我們就可以用commit、rollback來控制事務了。
?
在一段語句塊中禁用autocommit 而不是set autocommit
start transaction ;
select @ result := avg (age) from temp;
update temp set age = @ result where id = 2;
select * from temp where id = 2; //值被改變
rollback ; //回滾
select * from temp where id = 2; //變回來了
在此期間只有遇到commit、 rollback , start Transaction的禁用autocommit才會結束。然后就恢復到原來的autocommit模式;
?
不能回滾的語句
有些語句不能被回滾。通常,這些語句包括數據定義語言(DDL)語句,比如創建或取消數據庫的語句,
和創建、取消或更改表或存儲的子程序的語句。
您在設計事務時,不應包含這類語句。如果您在事務的前部中發布了一個不能被回滾的語句,
則后部的其它語句會發生錯誤,在這些情況下,通過發布ROLLBACK語句不能 回滾事務的全部效果。
?
一些操作也會隱式的提交事務
如alter、 create 、 drop 、rename table 、lock table 、 set autocommit、 start transaction 、 truncate table 等等,
在事務中出現這些語句也會提交事務的
事務不能嵌套事務
事務的保存點
Savepoint pointName/ Rollback to savepoint pointName
一個事務可以設置多個保存點,rollback可以回滾到指定的保存點,恢復保存點后面的操作。
如果有后面的保存點和前面的同名,則刪除前面的保存點。
Release savepoint會刪除一個保存點,如果在一段事務中執行commit或rollback,則事務結束,所以保存點刪除。
?
Set
Transaction設計數據庫隔離級別
SET [ GLOBAL | SESSION ] TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE }
本語句用于設置事務隔離等級,用于下一個事務,或者用于當前會話。
在默認情況下,
SET
TRANSACTION會為下一個事務(還未開始)設置隔離等級。
如果您使用GLOBAL關鍵詞,則語句會設置全局性的默認事務等級,
用于從該點以后創建的所有新連接。原有的連接不受影響。使用SESSION關鍵測可以設置默認事務等級,
用于對當前連接執行的所有將來事務。
默認的等級是REPEATABLE READ全局隔離等級。
?
? 注釋
select
1+1; # 單行注釋
select 1+1; -- 單行注釋
select 1 /* 多行注釋 */ + 1;
? 基本數據類型操作
字符串
select 'hello' , '"hello"' , '""hello""' , 'hel' 'lo' , '\' hello ';
select "hello", "'hello '", "' 'hello' '", "hel""lo", "\"hello";
?
\n換行
select 'This\nIs\nFour\nLines
';
?
\轉義
select 'hello \ world!
';
select 'hello \world!
';
select 'hello \\ world!
';
select 'hello \
' world!'
;
? 設置數據庫mode模式
SET sql_mode= 'ANSI_QUOTES' ;
create table t(a int );
create table "tt"(a int );
create table "t""t"(a int );
craate talbe tab("a""b"
int
);
? 用戶變量
set @num1 = 0, @num2 = 2, @ result = 0;
select @ result := (@num1 := 5) + @num2 := 3, @num1, @num2, @ result ;
? 存儲過程
創建存儲過程:
delimiter
//
create procedure get ( out result int )
begin
select max (age) into result from temp;
end //
調用存儲過程:
call get (@temp);
查詢結果:
select
@temp;
?
刪除存儲過程:
drop procedure get ;
?
查看存儲過程創建語句:
show create procedure get ;
?
select … into 可以完成單行記錄的賦值:
create procedure getRecord(sid int )
begin
declare v_name varchar (20) default 'jason' ;
declare v_age int ;
declare v_sex bit ;
select name, age, sex into v_name, v_age, v_sex from temp where id = sid;
select
v_name, v_age, v_sex;
end
;
call
getRecord(1);
? 函數
函數類似于存儲過程,只是調用方式不同
例如: select max (age) from temp;
?
創建函數:
create function addAge(age int ) returns int
return
age + 5;
?
使用函數:
select addAge(age) from temp;
?
刪除函數:
drop function if exists addAge;
drop function addAge;
?
顯示創建語法:
show create function addAge;
? 游標
聲明游標: declare cur_Name cursor for select name from temp;
打開游標:
open
cur_Name;
Fetch游標: fetch cur_Name into @temp;
關閉游標:
close
cur_Name;
?
示例:
CREATE PROCEDURE cur_show()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE v_id, v_age INT ;
DECLARE v_name varchar (20);
DECLARE cur_temp CURSOR FOR SELECT id, name, age FROM temp;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN
cur_temp;
REPEAT
FETCH cur_temp INTO v_id, v_name, v_age;
IF NOT done THEN
IF isnull(v_name) THEN
update temp set name = concat( 'test-json' , v_id) where id = v_id;
ELSEIF isnull(v_age)
THEN
update temp set age = 22 where id = v_id;
END IF ;
END IF ;
UNTIL done
END
REPEAT;
CLOSE
cur_temp;
END
? 觸發器
觸發器分為insert、
update
、delete三種觸發器事件類型
還有after、before觸發時間
創建觸發器:
create trigger trg_temp_ins
before
insert
on temp for each row
begin
insert into temp_log values ( NEW .id, NEW .name);
end //
?
刪除觸發器:
drop trigger trg_temp_ins
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
