mysql高級語句的查詢語句使用及說明
一、排序語法,關(guān)鍵字排序

升序和降序
默認(rèn)的排序方式就是升序
- 升序:ASC
- 降序:DESC
- 配合語法:order by 語法
1、升序
select * from info order by name; 根據(jù)名字升序排序,不需要加ASC

select * from info order by id;根據(jù)id升序排序

2、 降序(指定列)
select * from info order by name desc;根據(jù)名字降序

select * from info order by id desc; 根據(jù)id降序

3、多個(gè)列排序
以多個(gè)列作為排序關(guān)鍵字,只有第一個(gè)參數(shù)有相同的值第二個(gè)字段才有意義。
select * from info order by hobbid desc,id;
重復(fù)值對應(yīng)的第二個(gè)字段按照指定的排序進(jìn)行排序

select * from info order by id desc,score;#根據(jù)id降序,成績升序

二、區(qū)間判斷
1、and 且
兩個(gè)條件都要滿足
select * from info where score > 70 and score <=90; 成績大于70且小于90

2、or 且
兩個(gè)條件只要滿足一個(gè)即可
select * from info where score > 70 OR score <=90;成績大于70或者成績小于90
符合條件的都展示出來

3、嵌套多條件
select * from info where score > 70 or ( score > 0 and score < 20 ); 查找成績大于70 或者 成績大于0且成績小于20

三、分組查詢
對sql查詢的結(jié)果進(jìn)行分組,使用group by 語句來實(shí)現(xiàn)
group by 語句配合聚合函數(shù)一起使用。
聚合函數(shù)的類型:統(tǒng)計(jì) count,求和 sum,求平均數(shù) avg,最大值max,最小值 min。

select (指定列)count(name),hobbid(進(jìn)行分組) from info group by hobbi;
最少兩個(gè)列,group by 不能跟聚合函數(shù)后面的列

在聚合函數(shù)分組語句中,所有的非聚合函數(shù)列,都要在group by語句當(dāng)中。
select count(name),hobbid,name from info group by hobbid,name;

select count(name),hobbid,name from info where score >=80 GROUP BY hobbid,name; 以愛好為分組,統(tǒng)計(jì)成績大于80的名字

GROUP BY為一個(gè)整體,在其前面可以使用where,在其后面要使用having
select count(name),hobbid,score from info GROUP BY hobbid,score having score >=80;

select avg(score),hobbid from info group by hobbid having avg(score) >= 60;

統(tǒng)計(jì)姓名,以興趣和分?jǐn)?shù)作為分組,統(tǒng)計(jì)出成績大于80的,然后按照降序?qū)π彰牧羞M(jìn)行排序
select count(name),hobbid,score from info group by hobbid,score having score > 80 order by count(name) desc;

四、偏移量
limit 1,3 1是位置偏移量(可選參數(shù))
如果不設(shè)定位置偏移量,默認(rèn)就是從第一行開始(0+1),默認(rèn)值是0
select * from info limit 3; 顯示前三行

select * from info limit 1,3; 顯示2-4行

使用limit 和降序,只顯示最后最后三行
select * from info order by id desc limit 3;

五、別名as
表和列的別名:因?yàn)樵趯?shí)際工作中,表的名字和列的名字可能會很長,書寫起來不太方便,多次聲明表和列時(shí),完整的展示太復(fù)雜了,設(shè)置別名可以使書寫簡化了可讀性增加了,簡潔明了。
格式一:
select name as 姓名,score as 成績 from info;

格式二:
select name 姓名,score 成績 from info;

格式三:
select i.name 姓名,i.score 成績 from info i;

格式四:
select i.name 姓名,i.score 成績 from info as i;

六、 對表進(jìn)行復(fù)制
create table test as select * from info;
只能復(fù)制表的數(shù)據(jù),不能復(fù)制表的結(jié)構(gòu),主鍵的約束復(fù)制不了
desc info;查看區(qū)別

desc test;查看復(fù)制的表

用條件方式復(fù)制表
create table test1 as select * from info where score >=60;
test表

七、通配符
- like :模糊查詢
- % :表示0個(gè),1個(gè) 或者多個(gè)字符類似于*
- _:表示單個(gè)字符。

select * from info where address like 's%';

八、子查詢
子查詢: 內(nèi)查詢,嵌套查詢,select語句當(dāng)中又嵌套了一個(gè)select。
嵌套的select才是子查詢,先執(zhí)行子查詢的語句,外部的select在根據(jù)子條件的結(jié)果進(jìn)行過濾查找。
子查詢可以是多個(gè)表,也可以是同一張表
關(guān)聯(lián)語句 in not in
select(select)
select id,name,score from info where id in(select id from info where score>=80); 先將子查詢的表的內(nèi)容查詢出來

多張表查詢
select id,name,score from info where id in (select id from test where score>=80);

select id,name,score from info where id not in (select id from test where score>=80);

update info set score=80 where id in (select id from test where id =2); 根據(jù)test表中的id=2,然后更新info表中id=2的分?jǐn)?shù)
info表修改前

info表修改后

在子查詢當(dāng)中多表查詢(不要超過三張)和別名:
info表

test表

info表和test表,這兩張表id部分相同,然后根據(jù)id相同的部分來查詢info表的id的值
select a.id from info a where id in (select b.id from test b where a.id = b.id );

查詢出info表成績大于80的數(shù)據(jù)
select a.name from info a where a.score>80 and a.id in (select b.id from test b where a.id = b.id );

求出平均數(shù)
select avg(a.score) from info a where a.id in (select b.id from test b where a.id = b.id );

九、exists
exists 判斷子查詢的結(jié)果是否為空,不為空返回true,空返回false
select count(*)from info where exists (select id from test where score>80);
這里不是in和not in會傳給主表。
這里只是判斷條件,存在才執(zhí)行,不存在,結(jié)果為空則不執(zhí)行。
這里的值不是計(jì)數(shù)的作用

查詢分?jǐn)?shù),如果分?jǐn)?shù)小于50的則統(tǒng)計(jì)info的字段數(shù)
select count(*) from info where exists (select id from info where score<50);

select id from info where score<50查詢這個(gè)條件存不存在,僅僅是一個(gè)判斷的條件,并不是將結(jié)果傳到下一個(gè)命令中,結(jié)果存在執(zhí)行此條命令select count(*) from info,若不存在則不執(zhí)行。
十、視圖
視圖是一個(gè)虛擬表,表的數(shù)據(jù)是基于查詢的結(jié)果生成的,視圖可以簡化復(fù)雜的查詢,隱藏復(fù)雜的細(xì)節(jié),訪問數(shù)據(jù)更安全。
視圖是多表數(shù)據(jù)的集合體。
1、視圖和表之間的區(qū)別
- 存儲方式,表是實(shí)際的數(shù)據(jù)行,視圖不存儲數(shù)據(jù),僅僅是查詢結(jié)果的虛擬表
- 數(shù)據(jù)更新,更新表可以直接更新視圖表的數(shù)據(jù)
- 占用空間,表實(shí)際占用空間,視圖表不占用空間,只是一個(gè)動(dòng)態(tài)結(jié)果的展示。
視圖表的數(shù)據(jù)可能是一張表的部分查詢數(shù)據(jù),也可能是多個(gè)表的一部分查詢數(shù)據(jù)。
2、查看當(dāng)前數(shù)據(jù)庫中的視圖表
show full tables in xy102 where table_type like 'VIEW';

create view test2 as select * from info where score >=80;

查詢select * from test2;實(shí)際上等于執(zhí)行select * from info where score >=80

當(dāng)修改表中id=1的分?jǐn)?shù)修改為79,視圖表執(zhí)行執(zhí)行select * from test2;

結(jié)果

當(dāng)修改視圖id=2的分?jǐn)?shù)修改為90時(shí),查看原表也被修改
5.5之前視圖只讀,5.5之后雙向的,修改視圖表的信息,原表是數(shù)據(jù)也會發(fā)生更改
視圖表:

原表:

創(chuàng)建一張視圖表,視圖表包含id name address,從info和test當(dāng)中的name值相同的部分創(chuàng)建
create view test4 as select a.id,a.name,a.address from info a where a.name in (select b.name from test b where a.name = b.name );

1、視圖就是查詢語句的別名,有了視圖表可以簡化查詢的語句
2、表的權(quán)限不一樣,庫的權(quán)限是有控制的,所以查詢視圖表的權(quán)限相對低
3、既可以保證原表的數(shù)據(jù)安全,也簡化了查詢的過程
刪除視圖表
drop view test4;
十一、 連接查詢
把兩張表或者多個(gè)表的記錄結(jié)合起來,基于這些表,共同的字段,進(jìn)行數(shù)據(jù)的拼接。
前提:要確定一個(gè)主表作為結(jié)果集,然后把其他表的行,有選擇性的選定到結(jié)果上。
test1表

test2表

連接的類型:
1、內(nèi)連接inner join
取兩張表或者多張表之間符合條件的數(shù)據(jù)記錄的集合。
取兩個(gè)表或者多個(gè)表之間的交集
select a.a_id,a.a_name from test1 a INNER JOIN test2 b on a.a_name=b.b_name;

select * from test1 a INNER JOIN test2 b on a.a_name=b.b_name;

2、左連接
左外連接,left jion .......on 或者left outer join
以左邊的表為基礎(chǔ),接收左表的所有行,以左表的記錄和右表的記錄進(jìn)行匹配
匹配左表的所有,以及右表中符合條件的行,不符合顯示null,不展示。
select * from test1 a left join test2 b on a.a_name=b.b_name;
寫在left左邊的為左表,在左邊中選擇name作為一個(gè)篩選的條件,只有結(jié)果相同的其顯示的都能顯示出來,否則是null。

以比較條件為標(biāo)準(zhǔn),兩個(gè)表相同的展示出來,并作拼接,不同的結(jié)果顯示null
3、右連接
右外連接,right jion .......on 或者right outer join
以右邊的表為基礎(chǔ),接收右表的所有行,以右表的記錄和左表的記錄進(jìn)行匹配
匹配右表的所有,以及左表中符合條件的行,不符合顯示null,不展示。
select * from test1 a right join test2 b on a.a_name=b.b_name;

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL部署后連接被拒絕問題的排查與解決方法詳細(xì)指南
MySQL是一個(gè)開源的、關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在開發(fā)過程中被廣泛使用,有時(shí)候我們可能會遇到MySQL連接不上本地服務(wù)器的問題,這篇文章主要介紹了MySQL部署后連接被拒絕問題的排查與解決方法,需要的朋友可以參考下2025-11-11
mysql?sql_mode數(shù)據(jù)驗(yàn)證檢查方法
sql_mode?會影響MySQL支持的sql語法以及執(zhí)行的數(shù)據(jù)驗(yàn)證檢查,通過設(shè)置sql_mode?,可以完成不同嚴(yán)格程度的數(shù)據(jù)校驗(yàn),有效地保障數(shù)據(jù)準(zhǔn)確性,這篇文章主要介紹了mysql?sql_mode數(shù)據(jù)驗(yàn)證檢查,需要的朋友可以參考下2023-08-08
MySQL中復(fù)制表結(jié)構(gòu)及其數(shù)據(jù)的5種方式
在MySQL中,復(fù)制表結(jié)構(gòu)及其數(shù)據(jù)可以通過多種方式實(shí)現(xiàn),每種方法都有其適用場景,選擇合適的方法可以提高工作效率,注意處理目標(biāo)表存在性、大表復(fù)制效率及外鍵等約束,感興趣的可以了解一下2024-09-09
30個(gè)mysql千萬級大數(shù)據(jù)SQL查詢優(yōu)化技巧詳解
本文總結(jié)了30個(gè)mysql千萬級大數(shù)據(jù)SQL查詢優(yōu)化技巧,特別適合大數(shù)據(jù)里的MYSQL使用2018-03-03
簡單了解mysql InnoDB MyISAM相關(guān)區(qū)別
這篇文章主要介紹了簡單了解mysql InnoDB MyISAM相關(guān)區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09

