MySQL一些常用高級(jí)SQL語(yǔ)句詳解
一、MySQL進(jìn)階查詢
首先先創(chuàng)建兩張表
mysql -u root -pXXX #登陸數(shù)據(jù)庫(kù),XXX為密碼 create database jiangsu; #新建一個(gè)名為jiangsu的數(shù)據(jù)庫(kù) use jiangsu; #使用該數(shù)據(jù)庫(kù) create table location(Region char(20),Store_name char(20)); #創(chuàng)建location表,字段1為Region,數(shù)據(jù)類型為char,數(shù)據(jù)長(zhǎng)度為20;字段2為Store_name,數(shù)據(jù)類型為char,長(zhǎng)度為20. insert into location values('North','Xuzhou'); #插入4條數(shù)據(jù) insert into location values('North','Suqian'); insert into location values('South','Nanjing'); insert into location values('South','Suzhou'); create table store_info(Store_name char(20),Sales int(10),Date char(10)); #再創(chuàng)建一張store_info表 insert into store_info values('Xuzhou',300,'2020-12-08'); #插入4條數(shù)據(jù) insert into store_info values('Suqian',249,'2020-12-07'); insert into store_info values('Nanjing',1500,'2020-12-05'); insert into store_info values('Suzhou',700,'2020-12-08');
----------------select-------------------
select用于查詢表格中的一個(gè)或多個(gè)字段的數(shù)據(jù)記錄
語(yǔ)法格式:select 字段1,字段2,... from 表名;
select * from location; #查詢location表中的所有字段,*表示所有,如果不嫌麻煩,當(dāng)然也可以將所有字段都輸進(jìn)去
select Region from location; #查詢location表中的Region的數(shù)據(jù)
select sales,Date from store_info; #查詢store_info表中的sales,Date字段的數(shù)據(jù)----------------DISTINCT-------------------
DISTINCT用于將重復(fù)的數(shù)據(jù)壓縮為一個(gè)
語(yǔ)法格式:select distinct 字段名 from 表名;
select distinct Store_name from store_info; #查詢dtore_info表中的Store_name字段的數(shù)據(jù),如有重復(fù),則只顯示一個(gè)----------------where-------------------
where用于帶條件查詢
語(yǔ)法格式:select 字段名 from 表名 where 條件語(yǔ)句;
select Store_name from store_info where sales=300; #查詢store_info表中sales字段的值等于300的Store_name的數(shù)據(jù)
select Store_name from store_info where sales>500; #查詢store_info表中sales字段的值大于500的Store_name的數(shù)據(jù)----------------and or-------------------
and,且,用于查詢一個(gè)數(shù)據(jù)范圍;or,或,用于查詢包含條件語(yǔ)句的所有數(shù)據(jù)
語(yǔ)法格式:select 字段名 from 表名 where 條件1 and/or 條件2;
select Store_name from store_info where sales>250 and sales<1000; #查詢store_info表中sales字段的值大于250,且小于1000的Store_name的數(shù)據(jù)
select Store_name from store_info where sales<250 or sales>1000; #查詢store_info表中sales字段的值小于250,或者 大于1000的Store_name的數(shù)據(jù)
select Store_name from store_info where sales>1000 or (sales >200 and sales < 500); #括號(hào)的優(yōu)先級(jí)高,所以先根據(jù)括號(hào)里的and條件語(yǔ)句進(jìn)行篩選,然后再根據(jù)or進(jìn)行篩選,最后查詢最終篩選出來(lái)的數(shù)據(jù);該語(yǔ)句先篩選出sales大于200且小于500的值,再使用or進(jìn)行或的刪選,最終篩選出來(lái)的結(jié)果應(yīng)該是在200到500之間的值或者大于1000的值。
select Store_name from store_info where sales>1000 or sales >200 and sales < 500; #如果不加括號(hào),and的優(yōu)先級(jí)是比or要高的,也就是說(shuō),當(dāng)一條條件語(yǔ)句中同時(shí)存在and和or(沒(méi)有括號(hào)),會(huì)先執(zhí)行and條件。----------------in-------------------
in用來(lái)顯示已知值的數(shù)據(jù),簡(jiǎn)單來(lái)說(shuō),in后面跟的是一個(gè)數(shù)據(jù)集合,查詢語(yǔ)句會(huì)根據(jù)數(shù)據(jù)集合中的值進(jìn)行篩選查詢。not in 就是取數(shù)據(jù)集合中的反,不在數(shù)據(jù)集合中的數(shù)據(jù)。
語(yǔ)法格式:select 字段名1 from 表名 where 字段名2 in ('字段名2的值1','字段名2的值2,......');
select * from store_info where Store_name in ('Nanjing','Xuzhou'); #將Nanjing和Xuzhou的所有信息都查詢出來(lái)。
注:in可以用or代替
上述語(yǔ)句等于:select * from store_info where Store_name='Nanjing' or Store_name='Xuzhou';----------------between...and-------------------
between 值1 and 值2 ,在值1與值2之間(值2 > 值1),該語(yǔ)句查詢的是一個(gè)范圍,包含值1和值2。其作用相在某一方面當(dāng)于大于等于 ... and 小于等于 ... 。
語(yǔ)法格式:select 字段名 from 表名 where 字段名 between 值1 and 值2;
select * from store_info where Date between '2020-12-07' and '2020-12-10'; #查詢store_info表中的Data的值在12-06與12-10之間的所有數(shù)據(jù)----------------通配符-------------------
通配符一般情況下和like一起使用進(jìn)行模糊查詢,模糊查詢的概念就是將所有符合條件的數(shù)據(jù)全部查詢出來(lái),而等于號(hào)是精確查詢,會(huì)直接將具體的某一數(shù)據(jù)查詢出來(lái)
模糊查詢的字符如下:
%:百分號(hào)表示0個(gè),1個(gè)或多個(gè)字符
_:下劃線表示單個(gè)字符
語(yǔ)法格式:select 字段 from 表名 where 字段 like '通配符';
select * from store_info where Date like '2020%'; #將Date的值為2020,后面隨便(2020后有沒(méi)有都行)的值全部查詢出來(lái)
select * from store_info where Date like '2020-12-0_'; #將2020-12-0,后面只能匹配一個(gè)字符(必須存在且只能有一個(gè))的所有數(shù)據(jù)查詢出來(lái)----------------like-------------------
like,模糊查詢,用于查詢符合條件的所有數(shù)據(jù),通常和通配符一起使用,語(yǔ)法和通配符一樣的,因?yàn)槭墙Y(jié)合使用。
create database name;
use name;
create table stu_name(sname char(10));
insert into stu_name values('張');
insert into stu_name values('張三');
insert into stu_name values('張四');
insert into stu_name values('張無(wú)忌');
insert into stu_name values('一張紙');
insert into stu_name values('弓長(zhǎng)張');
select * from stu_name where sname like '張%'; #查詢所有張姓的名字,只要姓張就行
select * from stu_name where sname like '%張'; #查詢所有最后一個(gè)字是張的姓名,前面無(wú)所謂
select * from stu_name where sname like '%張%'; #查詢所有包含張的姓名,張字在姓在名都行
select * from stu_name where sname like '張_'; #查詢所有張姓且只有兩個(gè)字的名字
select * from stu_name where sname like '張__';(兩條下劃線) #查詢所有張姓,且必須為三個(gè)字的名字
select * from stu_name where sname like '_張%'; #查詢所有第二個(gè)字為張的名字
select * from stu_name where sname like '張_%'; #查詢所有張姓,名字至少包含兩個(gè)字的名字
select * from stu_name where sname like '張%_'; #查詢所有張姓,名字至少包含兩個(gè)字的名字,該語(yǔ)句和上面的查詢結(jié)果一樣,但理解是不同的。----------------order by-------------------
order by 用于關(guān)鍵字的排序
語(yǔ)法格式:select 字段 from 表名 [where 條件語(yǔ)句] order by 字段 asc/desc;
asc:按字段升序,默認(rèn)為asc
desc:按字段降序
select Store_name,Date,sales from store_info order by sales; #按照sales升序排列后,查詢name、date和sales
select Store_name,Date,sales from store_info order by sales desc; #按照sales降序排列后,查詢name、date和sales
二、MySQL數(shù)據(jù)庫(kù)函數(shù)
-------------數(shù)學(xué)函數(shù)-------------------
abs(x) #返回x的絕對(duì)值
rand() #返回0到1之間的隨機(jī)數(shù)
mod(x,y) #返回x除以y的余數(shù)
power(x,y) #返回x的y次方
round(x) #返回離x最近的整數(shù)
round(x,y) #保留x的y位小數(shù)四舍五入之后的值
sqrt(x) #返回x的平方根
truncate(x,y) #返回x截?cái)酁閥位小數(shù)的值
ceil(x) #返回大于或等于x的最小整數(shù)
floor(x) #返回小于或等于x的最大整數(shù)
greatest(x,y,z,...) #返回集合中最大的值
least(x,y,z,...) #返回集合中最小的值
---------------------------------------------
select abs(-1),rand(),mod(5,2),power(2,3),round(1.75);
select round(3.1415926,5),sqrt(2),truncate(3.141592653,4),ceil(5.2),floor(3.2),greatest(1.61,2.54,0.87),least(5.23,8.71,4.13);
--------------聚合函數(shù)--------------------
avg() #返回指定列的平均值
count() #返回指定列中非空值的個(gè)數(shù)
min() #返回指定列的最小值
max() #返回指定列的最大值
sum(x) #返回指定列的所有值的和
------------------------------------------
select avg(sales) from store_info; #查詢sales的平均值
平均值的另一種方法:
select sum(sales)/(select count(sales) from store_info) from store_info;select count(Date) from store_info; #統(tǒng)計(jì)Date的數(shù)據(jù)個(gè)數(shù),包括重復(fù)的值,但不包括空值
select count(distinct Date) from store_info; #統(tǒng)計(jì)Date的數(shù)據(jù)個(gè)數(shù),重復(fù)的數(shù)據(jù)只統(tǒng)計(jì)一次,不包括空值
select count(*) from store_info; #全部統(tǒng)計(jì),包括空值,count(*)掃描全表
select min(sales) from store_info; #查詢sales的最小值
最小值的另一種方法:
select sales from store_info order by sales limit 1;select max(sales) from store_info; #查詢sales的最大值
最大值的另一種方法:
select sales from store_info order by sales desc limit 1;select sum(sales) from store_info; #查詢sales的和
-----------------字符串函數(shù)--------------------
trim() #返回去除指定格式的值
concat(x,y) #將提供的參數(shù)x和y拼接成一個(gè)字符串
substr(x,y) #獲取字符串x中第y個(gè)位置開(kāi)始的字符串,跟substring()函數(shù)作用相同
substr(x,y,z) #獲取字符串x中第y個(gè)位置開(kāi)始,長(zhǎng)度為z的字符串
length(x) #返回字符串x的長(zhǎng)度
replace(x,y,z) #將字符串z替代字符串x中的字符串y
upper(x) #將字符串x中的所有字母變成大寫(xiě)
lower(x) #將字符串x中的所有字母變成小寫(xiě)
left(x,y) #返回字符串x中的前y個(gè)字符
right(x,y) #返回字符串x中的后y個(gè)字符
repeat(x,y) #將字符串x重復(fù)y次
space(x) #返回x個(gè)空格
strcmp(x,y) #比較x和y,返回的值可以為-1,0,1
reverse(x) #將字符串x反轉(zhuǎn)select concat(Region,Store_name) from location where Store_name='Xuzhou'; #將location表中Store_name='Xuzhou'的Region,Store_name的值拼接在一起
select Region || ' ' || Store_name from location where Store_name='Xuzhou'; #在my.cnf中開(kāi)啟了PIPES_AS_CONCAT模式后,可以使用 || 代替concat函數(shù),將多個(gè)字符串拼接在一起
select substr(Store_name,3) from store_info where Store_name='Suqian'; #將Suqian的第3個(gè)字符往后的所有字符截取出來(lái)
select substr((select Region || ' ' || Store_name from location where Store_name='Xuzhou'),1,5); #將上一條拼接的字段的第1到5個(gè)字符截取出來(lái)
select trim([[位置] [要除移的字符串] from] 字符串);
#位置:leading(開(kāi)頭),tariling(結(jié)尾),both(開(kāi)頭及結(jié)尾)
#要除移的字符串:從字符串的開(kāi)頭、結(jié)尾,或開(kāi)頭及結(jié)尾除移的字符串,缺省時(shí)為空格
select trim(leading 'Xu' from 'Xuzhou'); #將Xuzhou開(kāi)頭的Xu去掉select Region,length(Store_name) from location; #查詢location表中的Region字段的值和Store_name的值的長(zhǎng)度
select replace(Region,'th','thern') from location; #將location表中的Region字段的值包含th的替換為thern,然后返回。
------------------group by-------------------
group by用于對(duì)查詢結(jié)果進(jìn)行匯總分組,通常是結(jié)合聚合函數(shù)
一起使用,group by有一個(gè)原則,凡是在group by后面出現(xiàn)的
字段,必須在select后面出現(xiàn)。凡是在select后面出現(xiàn)、且未在
group by后面出現(xiàn)的字段,必須出現(xiàn)在group by后面。
語(yǔ)法格式:select 字段1,sum(字段2) from 表名 group by 字段1;select Store_name,sum(sales) from store_info group by Store_name order by sales;
------------------having----------------------
having用來(lái)過(guò)濾由group by語(yǔ)句返回的記錄值,通常與group by語(yǔ)句聯(lián)合使用。having語(yǔ)句的存在彌補(bǔ)了where關(guān)鍵字不能與聚合函數(shù)聯(lián)合使用的不足。
語(yǔ)法格式:select 字段1,sum(字段2) from 表名 group by 字段1 having (函數(shù)條件);
select Store_name,sum(sales) from store_info group by Store_name having sum(sales) >1000;-------------------別名----------------------
別名包括字段別名和表的別名,當(dāng)一張表的名字或者表中的某一個(gè)字段名過(guò)于冗長(zhǎng)時(shí),可以使用別名將之代替,從而降低查詢的失誤率。
語(yǔ)法格式:select 字段 [AS] 字段別名 from 表名 [AS] 表格別名;
select Store_name NAME,sales SALE from store_info;------------------子查詢---------------------
子查詢通常用于連接表格,在where子句或者h(yuǎn)aving子句中插入另一個(gè)SQL語(yǔ)句。
語(yǔ)法格式:select 字段1 from 表格1 where 字段2 [比較運(yùn)算符] (select 字段2 from 表格2 where 條件語(yǔ)句);括號(hào)里的select語(yǔ)句是內(nèi)查詢,括號(hào)外的select語(yǔ)句是外查詢
select Region from location where Store_name=(select Store_name from store_info where sales=300); #比較運(yùn)算符,可以是=、>、<或者>=、<=select sales from store_info where Store_name in (select Store_name from location); #也可以是in、between...and、like等
------------------exists---------------------
exists用來(lái)測(cè)試內(nèi)查詢有沒(méi)有產(chǎn)生任何結(jié)果,類似布爾值
是否為真。如果內(nèi)查詢產(chǎn)生了結(jié)果,則將結(jié)果作為外查詢
的條件繼續(xù)執(zhí)行,如果沒(méi)有結(jié)果,那么整條語(yǔ)句都不會(huì)產(chǎn)
生結(jié)果。
語(yǔ)法格式:select 字段1 from 表1 where exists (select 字段2 from 表2 where 條件語(yǔ)句);
select sum(sales) from store_info where exists (select * from location where Region='North'); #
#先執(zhí)行內(nèi)查詢語(yǔ)句,即在location表中查詢Region為North的所有數(shù)據(jù),如果存在,則執(zhí)行外查詢語(yǔ)句;如果不存在,就不執(zhí)行外查詢
------------------連接查詢-----------------------
inner join(內(nèi)連接):內(nèi)連接只返回兩個(gè)表中字段的值相等的數(shù)據(jù),即兩表的交集
left join(左連接):返回包括左表中的所有記錄和右表中聯(lián)結(jié)字段相等的記錄
right join(右連接):返回包括右表中的所有記錄和左表中聯(lián)結(jié)字段相等的記錄
select * from location left join store_info on location.Store_name=store_info.Store_name; #左連接select * from location right join store_info on location.Store_name=store_info.Store_name; #右連接
select * from location inner join store_info on location.Store_name=store_info.Store_name; #內(nèi)連接法1
SELECT * FROM location A, store_info B WHERE A.Store_Name = B.Store_Name; #內(nèi)連接法2
SELECT Region REGION, SUM(B.Sales) SALES FROM location A, store_info B WHERE A.Store_Name = B.Store_Name GROUP BY Region; #查詢兩表中name值相等的Region的值和sales的和,并按照Region字段進(jìn)行分組,REGION是字段Region的別名,SALES是sum(sales)的別名,A是表location的別名,B是info表的別名
---------------------view-----------------------
view,視圖,視圖是一張?zhí)摂M的表,通常用于保存多表聯(lián)合查詢出來(lái)數(shù)據(jù),這樣可以極大的減輕SQL語(yǔ)句的復(fù)雜度,在執(zhí)行n張表的聯(lián)合查詢時(shí),視圖可以起到很大的便利作用。視圖跟表格的不同是,表格中有實(shí)際儲(chǔ)存數(shù)據(jù)記錄,而視圖是建立在表格之上的一個(gè)架構(gòu),它本身并不實(shí)際儲(chǔ)存數(shù)據(jù)記錄。而視圖不會(huì)因?yàn)橥顺鰯?shù)據(jù)庫(kù)而消失。
語(yǔ)法格式1:create view 視圖名 as 查詢語(yǔ)句; #創(chuàng)建視圖
語(yǔ)法格式2:drop view 視圖名; #刪除視圖
show tables from 庫(kù)名; #該命令不僅可以查看庫(kù)所包含的表,也可以查看有哪些視圖------------------------union-------------------------
union,聯(lián)集,其作用是將兩個(gè)SQL語(yǔ)句的結(jié)果合并起來(lái),兩個(gè)SQL語(yǔ)句所產(chǎn)生的字段需要是同樣的數(shù)據(jù)記錄種類
union :生成結(jié)果的數(shù)據(jù)記錄值將沒(méi)有重復(fù),且按照字段的順序進(jìn)行排序
union all :將生成結(jié)果的數(shù)據(jù)記錄值都列出來(lái),無(wú)論有無(wú)重復(fù)
語(yǔ)法格式:[select 語(yǔ)句 1] UNION [all] [SELECT 語(yǔ)句 2];
select Store_name from location union select Store_name from store_info;select Store_name from location union all select Store_name from store_info;
-------------------求交集的幾種方法------------------
存在重復(fù)數(shù)據(jù):
1.select A.Store_Name FROM location A inner join store_info B on A.Store_Name = B.Store_Name;
2.select A.Store_name from location A,store_info B where A.Store_name=B.Store_name;
3.select A.Store_name from location A inner join store_info B using (Store_name);無(wú)重復(fù)數(shù)據(jù):
1.想要得到無(wú)重復(fù)數(shù)據(jù)其實(shí)很簡(jiǎn)單,在重復(fù)數(shù)據(jù)的查詢語(yǔ)句select后加上distinct去重即可。
select distinct A.Store_Name FROM location A inner join store_info B on A.Store_Name = B.Store_Name;2.使用子查詢,在內(nèi)查詢中查詢info中的name字段,并將之作為外查詢的條件,這樣,外查詢的查詢語(yǔ)句的范圍就只能在內(nèi)查詢查出的數(shù)據(jù)中進(jìn)行。(子查詢實(shí)際上就是變相的內(nèi)查詢)
select distinct Store_name from location where Store_name in (select Store_name from store_info);3.使用左查詢,將location和info表進(jìn)行左查詢,會(huì)查詢出location表中的所有name字段的值以及info表中與location表中name的值相等的數(shù)據(jù),再使用distinct進(jìn)行去重
select distinct A.Store_name from location A left join store_info B using(Store_name) where B.Store_name is not null;4.使用級(jí)聯(lián)查詢,union all會(huì)將兩張表的所有數(shù)據(jù)都連接到一起,這時(shí)只需要通過(guò)count()函數(shù)將大于1的數(shù)值統(tǒng)計(jì)出來(lái),即可實(shí)現(xiàn)查詢兩表的共同數(shù)值。
select Store_name,count(A.Store_name) from (select Store_name from location union all select Store_name from store_info) A group by A.Store_name having count(A.Store_name)>1 ;---------------------無(wú)交集---------------------
既然我們可以查詢出有交集的數(shù)據(jù),那么取反就可以實(shí)現(xiàn)無(wú)交集的查詢了
select distinct Store_name from location where Store_name not in (select Store_name from store_info);
---------------------case---------------------
case是SQL語(yǔ)句用來(lái)做為when-then-else(當(dāng)...就...否則...)之類邏輯的關(guān)鍵字
語(yǔ)法格式:
select case 字段名
when 條件1 then 結(jié)果1
when 條件2 then 條件2
.....
[else 結(jié)果n]
end
from 表名;
--------空值(NULL) 和 無(wú)值('') 的區(qū)別----------
1.無(wú)值的長(zhǎng)度為 0,不占用空間的;而 NULL 值的長(zhǎng)度是 NULL,是占用空間的。
2.IS NULL 或者 IS NOT NULL,是用來(lái)判斷字段是不是為 NULL 或者不是 NULL,不能查出是不是無(wú)值的。
3.無(wú)值的判斷使用=''或者<>''來(lái)處理。<> 代表不等于。
4.在通過(guò) count()指定字段統(tǒng)計(jì)有多少行數(shù)時(shí),如果遇到 NULL 值會(huì)自動(dòng)忽略掉,遇到無(wú)值會(huì)加入到記錄中進(jìn)行計(jì)算。
create table city(name char(10)); #新建city表
insert into city values ('beijing'); #插入三個(gè)值
insert into city values ('nanjing');
insert into city values ('xuzhou');
insert into city values (''); #插入兩個(gè)無(wú)值
insert into city values ('');
insert into city values (null); #插入兩個(gè)空值
insert into city values (null);
select * from city; #查詢city表的所有值
select length(name) from city; #查詢name字段值的長(zhǎng)度
select count(name) from city; #統(tǒng)計(jì)name字段的值,空值會(huì)被忽略
select length('111'),length(null),length(''); #比較有值、空值、無(wú)值的長(zhǎng)度
select * from city where name is null; #查詢name為空的值
select * from city where name is not null; #查詢name字段不為空的值
select * from city where name = ''; #查詢name值為無(wú)值的數(shù)據(jù)
select * from city where name <> ''; #查詢name字段不為無(wú)值的數(shù)據(jù),空值會(huì)被忽略
--------------------正則表達(dá)式-------------------------
^:匹配文本的開(kāi)始字符
$:匹配文本的結(jié)束字符
.:匹配任何一個(gè)字符
*:匹配零個(gè)或多個(gè)在它前面的字符
+:匹配前面的字符1次或多次
字符串:匹配包含指定的字符串
p1|p2:匹配p1或p2
[...]:匹配字符集合中的任意一個(gè)字符
[^...]:匹配不在括號(hào)中的任何字符
{n}:匹配前面的字符串 n 次
{n,m}:匹配前面的字符串至少n次,至多m次
語(yǔ)法格式:select 字段 from 表名 where 字段 REGEXP {模式};
select * from city where name regexp 'zhou'; #匹配數(shù)據(jù)中帶zhou的
select * from city where name regexp 'nan|bei'; #匹配數(shù)據(jù)中有nan或bei的
select * from city where name regexp '^[xnb]'; #匹配以xnb任一字符開(kāi)頭的數(shù)據(jù)
三、MySQL存儲(chǔ)過(guò)程
存儲(chǔ)過(guò)程是一組為了完成特定功能的SQL語(yǔ)句集合。
存儲(chǔ)過(guò)程在使用過(guò)程中是將常用或者復(fù)雜的工作預(yù)先使用SQL語(yǔ)句寫(xiě)好并用一個(gè)指定的名稱存儲(chǔ)起來(lái),這個(gè)過(guò)程經(jīng)編譯和優(yōu)化后存儲(chǔ)在數(shù)據(jù)庫(kù)服務(wù)器中。當(dāng)需要使用該存儲(chǔ)過(guò)程時(shí),只需要調(diào)用它即可。存儲(chǔ)過(guò)程在執(zhí)行上比傳統(tǒng)SQL速度更快、執(zhí)行效率更高。
存儲(chǔ)過(guò)程的優(yōu)點(diǎn):
1、執(zhí)行一次后,會(huì)將生成的二進(jìn)制代碼駐留緩沖區(qū),提高執(zhí)行效率
2、SQL語(yǔ)句加上控制語(yǔ)句的集合,靈活性高
3、在服務(wù)器端存儲(chǔ),客戶端調(diào)用時(shí),降低網(wǎng)絡(luò)負(fù)載
4、可多次重復(fù)被調(diào)用,可隨時(shí)修改,不影響客戶端調(diào)用
5、可完成所有的數(shù)據(jù)庫(kù)操作,也可控制數(shù)據(jù)庫(kù)的信息訪問(wèn)權(quán)限
------------------創(chuàng)建存儲(chǔ)過(guò)程-----------------------
delimiter !! #將語(yǔ)句的結(jié)束符號(hào)從分號(hào);臨時(shí)改為兩個(gè)?。?可以是自定義)
create procedure Proc() #創(chuàng)建存儲(chǔ)過(guò)程,過(guò)程名為Proc,不帶參數(shù)
-> begin #過(guò)程體以關(guān)鍵字 BEGIN 開(kāi)始
-> select * from Store_Info; #過(guò)程體語(yǔ)句
-> end $$ #過(guò)程體以關(guān)鍵字 END 結(jié)束
delimiter ; #將語(yǔ)句的結(jié)束符號(hào)恢復(fù)為分號(hào)------------------調(diào)用存儲(chǔ)過(guò)程-----------------------------
call Proc;
------------------查看存儲(chǔ)過(guò)程----------------------------
show create procrdure [數(shù)據(jù)庫(kù).]存儲(chǔ)過(guò)程名; #查看某個(gè)存儲(chǔ)過(guò)程的具體信息
show create procedure proc;
show procedure status [LIKE '%Proc%'] \G
----------------存儲(chǔ)過(guò)程的參數(shù)--------------------
IN 輸入?yún)?shù):表示調(diào)用者向過(guò)程傳入值(傳入值可以是字面量或變量)
OUT 輸出參數(shù):表示過(guò)程向調(diào)用者傳出值(可以返回多個(gè)值)(傳出值只能是變量)
INOUT 輸入輸出參數(shù):既表示調(diào)用者向過(guò)程傳入值,又表示過(guò)程向調(diào)用者傳出值(值只能是變量)實(shí)例:
delimiter !
create procedure proc1(in inname char(10))
-> begin
-> insert into city values(inname);
-> select * from city;
-> end !
delimiter ;
call proc1('hangzhou');------------------刪除存儲(chǔ)過(guò)程----------------------
存儲(chǔ)過(guò)程內(nèi)容的修改方法是通過(guò)刪除原有存儲(chǔ)過(guò)程,之后再以相同的名稱創(chuàng)建新的存儲(chǔ)過(guò)程。
drop procedure if exists 存儲(chǔ)過(guò)程名; #僅當(dāng)存在時(shí)刪除,不添加if exists時(shí),如果指定的過(guò)程不存在,則產(chǎn)生一個(gè)錯(cuò)誤
##存儲(chǔ)過(guò)程的控制語(yǔ)句##
create table num (id int);
insert into num values(10);
(1)條件語(yǔ)句if...then...else ...end if
delimiter !!
create procedure proc2(in nid int)
-> begin
-> declare var int; #declare 變量名 數(shù)據(jù)類型 來(lái)創(chuàng)建一個(gè)變量
-> set var=nid*3; #設(shè)置變量的參數(shù)
-> if var>=10 then #if-else判斷
-> update num set id=id+1;
-> else
-> update num set id=id-1;
-> end if; #結(jié)束判斷
-> end ?。?br />delimiter ;
call proc2(6);(2)循環(huán)語(yǔ)句while...do...end while #用法和if一樣,while的話必須要給定義的變量自增的過(guò)程,否則會(huì)死循環(huán),可以參考shell中的while語(yǔ)句
delimiter ??!
create procedure proc3()
-> begin
-> declare var int(10);
-> set var=0;
-> while var<6 do
-> insert into num values(var);
-> set var=var+1;
-> end while;
-> end ??!
delimiter ;
call proc3;
總結(jié)
- 各種函數(shù)的使用
- 多表查詢時(shí)的字段所屬的表
- 存儲(chǔ)過(guò)程的整體流程
- 靈活運(yùn)用所學(xué)知識(shí),舉一反三
到此這篇關(guān)于MySQL一些常用高級(jí)SQL語(yǔ)句詳解的文章就介紹到這了,更多相關(guān)MySQL SQL語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql中Table ‘XXX’ is marked as crashed and last (automatic?)
這篇文章主要介紹了Mysql中Table ‘XXX’ is marked as crashed and last (automatic?)問(wèn)題解決方法,需要的朋友可以參考下2014-05-05MySQL 5.0.96 for Windows x86 32位綠色精簡(jiǎn)版安裝教程
這篇文章主要介紹了MySQL 5.0.96 for Windows x86 32位綠色精簡(jiǎn)版安裝教程,需要的朋友可以參考下2017-10-10常見(jiàn)的十種SQL語(yǔ)句性能優(yōu)化策略詳解
這篇文章主要介紹了常見(jiàn)的十種SQL語(yǔ)句性能優(yōu)化策略詳解,SQL語(yǔ)句性能優(yōu)化是提高數(shù)據(jù)庫(kù)查詢效率的關(guān)鍵步驟,可以減少查詢時(shí)間,提高系統(tǒng)響應(yīng)速度,本文將介紹一些常見(jiàn)的SQL語(yǔ)句性能優(yōu)化技巧,包括索引的使用、合理的查詢條件、避免全表掃描等,需要的朋友可以參考下2023-10-10MySQL 查詢結(jié)果取交集的實(shí)現(xiàn)方法
本文將詳細(xì)介紹MySQL中如何實(shí)現(xiàn)以SQL查詢返回的結(jié)果集取交集的實(shí)現(xiàn)方法,需要的朋友可以參考2012-11-11MySQL數(shù)據(jù)庫(kù)的實(shí)時(shí)備份知識(shí)點(diǎn)詳解
本篇文章給大家分享了關(guān)于MySQL數(shù)據(jù)庫(kù)的實(shí)時(shí)備份知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以參考下。2018-08-08