最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL一些常用高級SQL語句詳解

 更新時間:2022年06月07日 10:01:45   作者:一念去殤  
對?MySQL?數(shù)據(jù)庫的查詢,除了基本的查詢外,有時候需要對查詢的結(jié)果集進(jìn)行處理。例如只取?10?條數(shù)據(jù)、對查詢結(jié)果進(jìn)行排序或分組等等,今天就給大家分享MySQL一些常用高級SQL語句,感興趣的朋友一起看看吧

一、MySQL進(jìn)階查詢

首先先創(chuàng)建兩張表

mysql -u root -pXXX     #登陸數(shù)據(jù)庫,XXX為密碼
create database jiangsu;   #新建一個名為jiangsu的數(shù)據(jù)庫
use jiangsu;   #使用該數(shù)據(jù)庫
create table location(Region char(20),Store_name char(20));   #創(chuàng)建location表,字段1為Region,數(shù)據(jù)類型為char,數(shù)據(jù)長度為20;字段2為Store_name,數(shù)據(jù)類型為char,長度為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用于查詢表格中的一個或多個字段的數(shù)據(jù)記錄
語法格式: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ù)壓縮為一個
語法格式:select distinct 字段名 from 表名;
select distinct Store_name from store_info;   #查詢dtore_info表中的Store_name字段的數(shù)據(jù),如有重復(fù),則只顯示一個

----------------where-------------------
where用于帶條件查詢
語法格式:select 字段名 from 表名 where 條件語句;
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,且,用于查詢一個數(shù)據(jù)范圍;or,或,用于查詢包含條件語句的所有數(shù)據(jù)
語法格式: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);   #括號的優(yōu)先級高,所以先根據(jù)括號里的and條件語句進(jìn)行篩選,然后再根據(jù)or進(jìn)行篩選,最后查詢最終篩選出來的數(shù)據(jù);該語句先篩選出sales大于200且小于500的值,再使用or進(jìn)行或的刪選,最終篩選出來的結(jié)果應(yīng)該是在200到500之間的值或者大于1000的值。
select Store_name from store_info where sales>1000 or sales >200 and sales < 500;   #如果不加括號,and的優(yōu)先級是比or要高的,也就是說,當(dāng)一條條件語句中同時存在and和or(沒有括號),會先執(zhí)行and條件。

----------------in-------------------
in用來顯示已知值的數(shù)據(jù),簡單來說,in后面跟的是一個數(shù)據(jù)集合,查詢語句會根據(jù)數(shù)據(jù)集合中的值進(jìn)行篩選查詢。not in 就是取數(shù)據(jù)集合中的反,不在數(shù)據(jù)集合中的數(shù)據(jù)。
語法格式:select 字段名1 from 表名 where 字段名2 in ('字段名2的值1','字段名2的值2,......');
select * from store_info where Store_name in ('Nanjing','Xuzhou');   #將Nanjing和Xuzhou的所有信息都查詢出來。
注:in可以用or代替
上述語句等于:select * from store_info where Store_name='Nanjing' or Store_name='Xuzhou';

----------------between...and-------------------
between 值1 and 值2 ,在值1與值2之間(值2 > 值1),該語句查詢的是一個范圍,包含值1和值2。其作用相在某一方面當(dāng)于大于等于 ... and 小于等于 ... 。
語法格式: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ù)全部查詢出來,而等于號是精確查詢,會直接將具體的某一數(shù)據(jù)查詢出來
模糊查詢的字符如下:
%:百分號表示0個,1個或多個字符
_:下劃線表示單個字符
語法格式:select 字段 from 表名 where 字段 like '通配符';
select * from store_info where Date like '2020%';   #將Date的值為2020,后面隨便(2020后有沒有都行)的值全部查詢出來
select * from store_info where Date like '2020-12-0_';   #將2020-12-0,后面只能匹配一個字符(必須存在且只能有一個)的所有數(shù)據(jù)查詢出來

----------------like-------------------
like,模糊查詢,用于查詢符合條件的所有數(shù)據(jù),通常和通配符一起使用,語法和通配符一樣的,因為是結(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('張無忌');
insert into stu_name values('一張紙');
insert into stu_name values('弓長張');
select * from stu_name where sname like '張%';   #查詢所有張姓的名字,只要姓張就行
select * from stu_name where sname like '%張';   #查詢所有最后一個字是張的姓名,前面無所謂
select * from stu_name where sname like '%張%';   #查詢所有包含張的姓名,張字在姓在名都行
select * from stu_name where sname like '張_';   #查詢所有張姓且只有兩個字的名字
select * from stu_name where sname like '張__';(兩條下劃線)  #查詢所有張姓,且必須為三個字的名字
select * from stu_name where sname like '_張%';  #查詢所有第二個字為張的名字
select * from stu_name where sname like '張_%';   #查詢所有張姓,名字至少包含兩個字的名字
select * from stu_name where sname like '張%_';   #查詢所有張姓,名字至少包含兩個字的名字,該語句和上面的查詢結(jié)果一樣,但理解是不同的。

----------------order by-------------------
order by 用于關(guān)鍵字的排序
語法格式:select 字段 from 表名 [where 條件語句] 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ù)庫函數(shù)

-------------數(shù)學(xué)函數(shù)-------------------
abs(x)         #返回x的絕對值
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截斷為y位小數(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()     #返回指定列中非空值的個數(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)計Date的數(shù)據(jù)個數(shù),包括重復(fù)的值,但不包括空值

select count(distinct Date) from store_info;   #統(tǒng)計Date的數(shù)據(jù)個數(shù),重復(fù)的數(shù)據(jù)只統(tǒng)計一次,不包括空值

select count(*) from store_info;   #全部統(tǒng)計,包括空值,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拼接成一個字符串
substr(x,y)        #獲取字符串x中第y個位置開始的字符串,跟substring()函數(shù)作用相同
substr(x,y,z)      #獲取字符串x中第y個位置開始,長度為z的字符串
length(x)          #返回字符串x的長度
replace(x,y,z)     #將字符串z替代字符串x中的字符串y
upper(x)           #將字符串x中的所有字母變成大寫
lower(x)           #將字符串x中的所有字母變成小寫
left(x,y)          #返回字符串x中的前y個字符
right(x,y)         #返回字符串x中的后y個字符
repeat(x,y)        #將字符串x重復(fù)y次
space(x)           #返回x個空格
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中開啟了PIPES_AS_CONCAT模式后,可以使用 || 代替concat函數(shù),將多個字符串拼接在一起

select substr(Store_name,3) from store_info where Store_name='Suqian';   #將Suqian的第3個字符往后的所有字符截取出來

select substr((select Region || ' ' || Store_name from location where Store_name='Xuzhou'),1,5);   #將上一條拼接的字段的第1到5個字符截取出來

select trim([[位置] [要除移的字符串] from] 字符串);
#位置:leading(開頭),tariling(結(jié)尾),both(開頭及結(jié)尾)
#要除移的字符串:從字符串的開頭、結(jié)尾,或開頭及結(jié)尾除移的字符串,缺省時為空格
select trim(leading 'Xu' from 'Xuzhou');   #將Xuzhou開頭的Xu去掉

select Region,length(Store_name) from location;   #查詢location表中的Region字段的值和Store_name的值的長度

select replace(Region,'th','thern') from location;   #將location表中的Region字段的值包含th的替換為thern,然后返回。

------------------group by-------------------
group by用于對查詢結(jié)果進(jìn)行匯總分組,通常是結(jié)合聚合函數(shù)
一起使用,group by有一個原則,凡是在group by后面出現(xiàn)的
字段,必須在select后面出現(xiàn)。凡是在select后面出現(xiàn)、且未在
group by后面出現(xiàn)的字段,必須出現(xiàn)在group by后面。
語法格式: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用來過濾由group by語句返回的記錄值,通常與group by語句聯(lián)合使用。having語句的存在彌補(bǔ)了where關(guān)鍵字不能與聚合函數(shù)聯(lián)合使用的不足。
語法格式: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)一張表的名字或者表中的某一個字段名過于冗長時,可以使用別名將之代替,從而降低查詢的失誤率。
語法格式:select 字段 [AS] 字段別名 from 表名 [AS] 表格別名;
select Store_name NAME,sales SALE from store_info;

------------------子查詢---------------------
子查詢通常用于連接表格,在where子句或者h(yuǎn)aving子句中插入另一個SQL語句。
語法格式:select 字段1 from 表格1 where 字段2 [比較運(yùn)算符] (select 字段2 from 表格2 where 條件語句);括號里的select語句是內(nèi)查詢,括號外的select語句是外查詢
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用來測試內(nèi)查詢有沒有產(chǎn)生任何結(jié)果,類似布爾值
 是否為真。如果內(nèi)查詢產(chǎn)生了結(jié)果,則將結(jié)果作為外查詢
 的條件繼續(xù)執(zhí)行,如果沒有結(jié)果,那么整條語句都不會產(chǎn)
 生結(jié)果。
 語法格式:select 字段1 from 表1 where exists (select 字段2 from 表2 where 條件語句);
 select sum(sales) from store_info where exists (select * from location where Region='North');   #
 #先執(zhí)行內(nèi)查詢語句,即在location表中查詢Region為North的所有數(shù)據(jù),如果存在,則執(zhí)行外查詢語句;如果不存在,就不執(zhí)行外查詢
------------------連接查詢-----------------------
inner join(內(nèi)連接):內(nèi)連接只返回兩個表中字段的值相等的數(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)合查詢出來數(shù)據(jù),這樣可以極大的減輕SQL語句的復(fù)雜度,在執(zhí)行n張表的聯(lián)合查詢時,視圖可以起到很大的便利作用。視圖跟表格的不同是,表格中有實際儲存數(shù)據(jù)記錄,而視圖是建立在表格之上的一個架構(gòu),它本身并不實際儲存數(shù)據(jù)記錄。而視圖不會因為退出數(shù)據(jù)庫而消失。
語法格式1:create view 視圖名 as 查詢語句;   #創(chuàng)建視圖
語法格式2:drop view 視圖名;   #刪除視圖
show tables from 庫名;   #該命令不僅可以查看庫所包含的表,也可以查看有哪些視圖

------------------------union-------------------------
union,聯(lián)集,其作用是將兩個SQL語句的結(jié)果合并起來,兩個SQL語句所產(chǎn)生的字段需要是同樣的數(shù)據(jù)記錄種類
union :生成結(jié)果的數(shù)據(jù)記錄值將沒有重復(fù),且按照字段的順序進(jìn)行排序
union all :將生成結(jié)果的數(shù)據(jù)記錄值都列出來,無論有無重復(fù)
語法格式:[select 語句 1] UNION [all] [SELECT 語句 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);

無重復(fù)數(shù)據(jù):
1.想要得到無重復(fù)數(shù)據(jù)其實很簡單,在重復(fù)數(shù)據(jù)的查詢語句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字段,并將之作為外查詢的條件,這樣,外查詢的查詢語句的范圍就只能在內(nèi)查詢查出的數(shù)據(jù)中進(jìn)行。(子查詢實際上就是變相的內(nèi)查詢)
select distinct Store_name from location where Store_name in (select Store_name from store_info);

3.使用左查詢,將location和info表進(jìn)行左查詢,會查詢出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.使用級聯(lián)查詢,union all會將兩張表的所有數(shù)據(jù)都連接到一起,這時只需要通過count()函數(shù)將大于1的數(shù)值統(tǒng)計出來,即可實現(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 ;

---------------------無交集---------------------
既然我們可以查詢出有交集的數(shù)據(jù),那么取反就可以實現(xiàn)無交集的查詢了
select distinct Store_name from location  where Store_name not in (select Store_name from store_info);

---------------------case---------------------
case是SQL語句用來做為when-then-else(當(dāng)...就...否則...)之類邏輯的關(guān)鍵字
語法格式:
select case 字段名
when 條件1 then 結(jié)果1
when 條件2 then 條件2
.....
[else 結(jié)果n]
end
from 表名;
 
 --------空值(NULL) 和 無值('') 的區(qū)別----------
1.無值的長度為 0,不占用空間的;而 NULL 值的長度是 NULL,是占用空間的。
2.IS NULL 或者 IS NOT NULL,是用來判斷字段是不是為 NULL 或者不是 NULL,不能查出是不是無值的。
3.無值的判斷使用=''或者<>''來處理。<> 代表不等于。 
4.在通過 count()指定字段統(tǒng)計有多少行數(shù)時,如果遇到 NULL 值會自動忽略掉,遇到無值會加入到記錄中進(jìn)行計算。
create table city(name char(10));   #新建city表
insert into city values ('beijing');   #插入三個值
insert into city values ('nanjing');
insert into city values ('xuzhou');
insert into city values ('');   #插入兩個無值
insert into city values ('');
insert into city values (null);   #插入兩個空值
insert into city values (null);
select * from city;   #查詢city表的所有值
select length(name) from city;   #查詢name字段值的長度
select count(name) from city;   #統(tǒng)計name字段的值,空值會被忽略
select length('111'),length(null),length('');   #比較有值、空值、無值的長度
select * from city where name is null;   #查詢name為空的值
select * from city where name is not null;   #查詢name字段不為空的值
select * from city where name = '';   #查詢name值為無值的數(shù)據(jù)
select * from city where name <> '';   #查詢name字段不為無值的數(shù)據(jù),空值會被忽略

--------------------正則表達(dá)式-------------------------
^:匹配文本的開始字符
$:匹配文本的結(jié)束字符
.:匹配任何一個字符
*:匹配零個或多個在它前面的字符
+:匹配前面的字符1次或多次
字符串:匹配包含指定的字符串                     
p1|p2:匹配p1或p2                             
[...]:匹配字符集合中的任意一個字符             
[^...]:匹配不在括號中的任何字符                 
{n}:匹配前面的字符串 n 次 
{n,m}:匹配前面的字符串至少n次,至多m次
語法格式: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任一字符開頭的數(shù)據(jù) 

三、MySQL存儲過程

存儲過程是一組為了完成特定功能的SQL語句集合。

存儲過程在使用過程中是將常用或者復(fù)雜的工作預(yù)先使用SQL語句寫好并用一個指定的名稱存儲起來,這個過程經(jīng)編譯和優(yōu)化后存儲在數(shù)據(jù)庫服務(wù)器中。當(dāng)需要使用該存儲過程時,只需要調(diào)用它即可。存儲過程在執(zhí)行上比傳統(tǒng)SQL速度更快、執(zhí)行效率更高。

存儲過程的優(yōu)點(diǎn):

1、執(zhí)行一次后,會將生成的二進(jìn)制代碼駐留緩沖區(qū),提高執(zhí)行效率

2、SQL語句加上控制語句的集合,靈活性高

3、在服務(wù)器端存儲,客戶端調(diào)用時,降低網(wǎng)絡(luò)負(fù)載

4、可多次重復(fù)被調(diào)用,可隨時修改,不影響客戶端調(diào)用

5、可完成所有的數(shù)據(jù)庫操作,也可控制數(shù)據(jù)庫的信息訪問權(quán)限

------------------創(chuàng)建存儲過程-----------------------
delimiter ??!  #將語句的結(jié)束符號從分號;臨時改為兩個?。?可以是自定義)
create procedure Proc()   #創(chuàng)建存儲過程,過程名為Proc,不帶參數(shù)
-> begin   #過程體以關(guān)鍵字 BEGIN 開始
-> select * from Store_Info;   #過程體語句
-> end $$   #過程體以關(guān)鍵字 END 結(jié)束
delimiter ;       #將語句的結(jié)束符號恢復(fù)為分號

------------------調(diào)用存儲過程-----------------------------
call Proc;
------------------查看存儲過程----------------------------
show create procrdure [數(shù)據(jù)庫.]存儲過程名;       #查看某個存儲過程的具體信息
show create procedure proc;
show procedure status [LIKE '%Proc%'] \G

----------------存儲過程的參數(shù)--------------------
IN 輸入?yún)?shù):表示調(diào)用者向過程傳入值(傳入值可以是字面量或變量)
OUT 輸出參數(shù):表示過程向調(diào)用者傳出值(可以返回多個值)(傳出值只能是變量)
INOUT 輸入輸出參數(shù):既表示調(diào)用者向過程傳入值,又表示過程向調(diào)用者傳出值(值只能是變量)

實例:
delimiter !
create procedure proc1(in inname char(10))
-> begin
-> insert into city values(inname);
-> select * from city;
-> end !
delimiter ;
call proc1('hangzhou');

------------------刪除存儲過程----------------------
存儲過程內(nèi)容的修改方法是通過刪除原有存儲過程,之后再以相同的名稱創(chuàng)建新的存儲過程。
drop procedure if exists 存儲過程名;        #僅當(dāng)存在時刪除,不添加if exists時,如果指定的過程不存在,則產(chǎn)生一個錯誤

##存儲過程的控制語句##
create table num (id int);
insert into num values(10);
(1)條件語句if...then...else ...end if 
delimiter ?。?nbsp;
create procedure proc2(in nid int)  
-> begin 
-> declare var int;   #declare 變量名 數(shù)據(jù)類型 來創(chuàng)建一個變量
-> 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)語句while...do...end while   #用法和if一樣,while的話必須要給定義的變量自增的過程,否則會死循環(huán),可以參考shell中的while語句
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 ?。?nbsp;
delimiter ;
call proc3;

總結(jié)

  • 各種函數(shù)的使用
  • 多表查詢時的字段所屬的表
  • 存儲過程的整體流程
  • 靈活運(yùn)用所學(xué)知識,舉一反三

到此這篇關(guān)于MySQL一些常用高級SQL語句詳解的文章就介紹到這了,更多相關(guān)MySQL SQL語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文弄懂MySQL自增主鍵

    一文弄懂MySQL自增主鍵

    這篇文章主要介紹了MySQL自增主鍵,MySQL的自增主鍵并不一定是連續(xù)的,自增值的保存位置和修改機(jī)制決定了一種自增值不連續(xù)的場景,下面就來具體介紹一下,感興趣的可以了解一下
    2025-01-01
  • mysql中rpm方式安裝的詳解

    mysql中rpm方式安裝的詳解

    在本文中小編給大家整理了關(guān)于mysql安裝之rpm方式安裝的詳細(xì)步驟以及注意點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-03-03
  • Mysql:The user specified as a definer (''xxx@''%'') does not exist的解決方案

    Mysql:The user specified as a definer (''xxx@''%'') does not

    今天小編就為大家分享一篇關(guān)于Mysql:The user specified as a definer ('xxx@'%') does not exist的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv

    詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv

    這篇文章主要介紹了 詳解java調(diào)用ffmpeg轉(zhuǎn)換視頻格式為flv的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • MySql利用父id遞歸向下查詢子節(jié)點(diǎn)的方法實例

    MySql利用父id遞歸向下查詢子節(jié)點(diǎn)的方法實例

    項目中遇到一個需求,要求查處菜單節(jié)點(diǎn)的所有節(jié)點(diǎn),在網(wǎng)上查了一下,大多數(shù)的方法用到了存儲過程,由于線上環(huán)境不能隨便添加存儲過程,所以自己寫一個,這篇文章主要給大家介紹了關(guān)于MySql利用父id遞歸向下查詢子節(jié)點(diǎn)的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • MySQL在Centos7環(huán)境安裝的完整步驟記錄

    MySQL在Centos7環(huán)境安裝的完整步驟記錄

    在CentOS7環(huán)境下安裝MySQL是一項常見的任務(wù),尤其對于那些沒有網(wǎng)絡(luò)連接或者需要在隔離環(huán)境中的開發(fā)者來說,離線安裝MySQL顯得尤為重要,這篇文章主要介紹了MySQL在Centos7環(huán)境安裝的完整步驟,需要的朋友可以參考下
    2024-10-10
  • 解決MySQL:Invalid GIS data provided to function st_geometryfromtext問題

    解決MySQL:Invalid GIS data provided to&nbs

    這篇文章主要介紹了解決MySQL:Invalid GIS data provided to function st_geometryfromtext問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Mysql存儲過程如何實現(xiàn)歷史數(shù)據(jù)遷移

    Mysql存儲過程如何實現(xiàn)歷史數(shù)據(jù)遷移

    這篇文章主要介紹了Mysql存儲過程如何實現(xiàn)歷史數(shù)據(jù)遷移,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • MySQL隱式轉(zhuǎn)換造成索引失效的解決辦法

    MySQL隱式轉(zhuǎn)換造成索引失效的解決辦法

    數(shù)據(jù)庫優(yōu)化是一個任重而道遠(yuǎn)的任務(wù),想要做優(yōu)化必須深入理解數(shù)據(jù)庫的各種特性,在開發(fā)過程中我們經(jīng)常會遇到一些原因很簡單但造成的后果卻很嚴(yán)重的疑難雜癥,這類問題往往還不容易定位,本文將給大家介紹MySQL隱式轉(zhuǎn)換造成索引失效的解決辦法,需要的朋友可以參考下
    2025-02-02
  • 在Linux環(huán)境下mysql的root密碼忘記解決方法(三種)

    在Linux環(huán)境下mysql的root密碼忘記解決方法(三種)

    這篇文章主要介紹了在Linux環(huán)境下mysql的root密碼忘記解決方法,詳細(xì)的介紹了3種解決辦法,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12

最新評論

忻城县| 巴彦县| 蒙自县| 武宣县| 建瓯市| 囊谦县| 临高县| 双城市| 八宿县| 翁牛特旗| 新津县| 饶平县| 富裕县| 玉门市| 桦甸市| 镇雄县| 博兴县| 米易县| 石河子市| 衡东县| 会东县| 武清区| 连州市| 左贡县| 安塞县| 邮箱| 黑河市| 桐乡市| 齐齐哈尔市| 牟定县| 景宁| 楚雄市| 筠连县| 梧州市| 兰考县| 垫江县| 通河县| 家居| 南岸区| 莱州市| 大庆市|