DQL數(shù)據(jù)查詢語句使用示例
DQL
(Data Query Language:數(shù)據(jù)查詢語言)
- 所有的查詢操作都要用到它 select
- 簡(jiǎn)單的查詢,復(fù)雜的查詢都要用到它
- 數(shù)據(jù)庫(kù)最核心的語言,最重要的語言
- 使用頻率最高的語言
指定查詢字段
-- 查詢所有的學(xué)生 select 字段 from 表 select * from student -- 查詢指定字段 select studentno,studentname from student -- 給結(jié)果起一個(gè)名字 , As,可以給字段起別名,也可以給表起別名 select studentno as 學(xué)號(hào),studentname as 學(xué)生姓名 from student select studentno 學(xué)號(hào),studentname 學(xué)生姓名 from student -- 函數(shù) concat(a,b) select CONCAT('姓名:',studentname) as 新名字 from student
語法:select 字段 from 表
有時(shí)候,列的名字不是那么的見名之意,我們可以用as給字段起別名
字段名 as 別名 或者是 表名 as 表別名
去重 distinct
作用:去除select查詢出來的結(jié)果中重復(fù)的數(shù)據(jù),重復(fù)的數(shù)據(jù)只顯示一條
-- 查詢一下有哪些同學(xué)參加了考試 select * from result -- 查詢?nèi)康目荚嚦煽?jī) select studentno from result -- 查詢有哪些學(xué)生參加了考試 select distinct studentno from result -- 去重
數(shù)據(jù)庫(kù)的列(表達(dá)式)
select version() -- 查詢系統(tǒng)版本號(hào)(函數(shù)) select 100*3-2 -- 用來計(jì)算(表達(dá)式) select @@auto_increment_increment -- 查詢自增的步長(zhǎng)(變量) -- 學(xué)生考試成績(jī)+1分查看 select studentno,studentresult+1 as '提分后' from result
數(shù)據(jù)庫(kù)中的表達(dá)式:文本值,列,null,函數(shù),計(jì)算表達(dá)式,系統(tǒng)變量
select 表達(dá)式 from 表
where條件子句
作用:檢索符合條件的值
搜索的條件都是由一個(gè)或者多個(gè)表達(dá)式組成!結(jié)果都是布爾值
邏輯運(yùn)算符
select studentno,studentresult from result -- 查詢考試成績(jī)?cè)?5~100之間的 -- and && select studentno,studentresult from result where studentresult>=95 and studentresult<=100 -- 模糊查詢(區(qū)間) select studentno,studentresult from result where studentresult between 95 and 100 -- ! not select studentno,studentresult from result where not studentno=1000
模糊查詢:比較運(yùn)算符
-- ===========模糊查詢======================== -- 查詢姓張的同學(xué) -- like結(jié)合%(代表0到任意個(gè)字符) _(一個(gè)字符) select studentno,studentname from student where studentname like '張%' -- 查詢姓張的同學(xué),名字只有兩個(gè)字 select studentno,studentname from student where studentname like '張_' -- ======in(具體的一個(gè)或者多個(gè)值)======== -- 查詢學(xué)號(hào)1000,1001的學(xué)生 select studentno,studentname from student where studentno in(1000,1001) -- 查詢?cè)诒本┑膶W(xué)生 select studentno,studentname from student where address in('北京朝陽') -- ========null not null==================== -- 查詢地址為空的同學(xué) null或者'' select studentno,studentname from student where address='' or address is null -- 查詢出生日期不為空的學(xué)生 select studentno,studentname from student where borndate is not null
聯(lián)表查詢
-- 查詢參加考試的同學(xué)(學(xué)號(hào),姓名,科目編號(hào),分?jǐn)?shù)) select * from student select * from result /*思路: 1.分析需求:分析查詢的字段來自哪些表(連接查詢) 2.確定使用哪種連接查詢? 7種 確定交叉點(diǎn),這兩個(gè)表中哪些數(shù)據(jù)是相同的 判斷的條件:學(xué)生表的studentno=成績(jī)表 studentno */ select s.studentno,studentname,subjectno,studentresult from student as s inner join result as r where s.studentno=r.studentno -- right join select s.studentno,studentname,subjectno,studentresult from student as s right join result as r on s.studentno = r.studentno -- left join select s.studentno,studentname,subjectno,studentresult from student as s left join result as r on s.studentno = r.studentno
練習(xí):
-- ===============聯(lián)表查詢================== -- 查詢參加考試的同學(xué)(學(xué)號(hào),姓名,科目編號(hào),分?jǐn)?shù)) select * from student select * from result /*思路: 1.分析需求:分析查詢的字段來自哪些表(連接查詢) 2.確定使用哪種連接查詢? 7種 確定交叉點(diǎn),這兩個(gè)表中哪些數(shù)據(jù)是相同的 判斷的條件:學(xué)生表的studentno=成績(jī)表 studentno */ -- join(連接的表) on(判斷的條件) 連接查詢 -- where 等值查詢 select s.studentno,studentname,subjectno,studentresult from student as s inner join result as r where s.studentno=r.studentno select s.studentno,studentname,subjectno,studentresult from student as s inner join result as r on s.studentno=r.studentno -- right join select s.studentno,studentname,subjectno,studentresult from student as s right join result as r on r.studentno = s.studentno -- left join select s.studentno,studentname,subjectno,studentresult from student as s left join result as r on s.studentno = r.studentno -- 查詢?nèi)笨嫉耐瑢W(xué)(只要讓成績(jī)?yōu)閚ull,就能找到) select s.studentno,studentname,subjectno,studentresult from student as s left join result as r on s.studentno = r.studentno where studentresult is null -- 查詢了參加考試的同學(xué)信息:學(xué)號(hào),學(xué)生姓名,科目名稱,分?jǐn)?shù) -- 表 student result subject -- 右查詢 -- 交叉點(diǎn):學(xué)生表 studentno=成績(jī)表 studentno -- 成績(jī)表 subjectno=科目表 subjectno select s.studentno,studentname,sub.subjectname,studentresult from student as s right join result as r on r.studentno=s.studentno inner join `subject` as sub on r.subjectno=sub.subjectno -- 我要查詢哪些數(shù)據(jù) select ... -- 從哪幾個(gè)表中查詢 from 表 xxx join 連接的表 on 較差條件 -- 假設(shè)存在一種多張表查詢,先查詢兩張表開始,然后再慢慢增加 -- from a left join b -- from a right join b
自連接
自己的表和自己的表連接;
核心:一張表拆分成兩張一樣的表即可
父類表:
子類表
查詢父類對(duì)應(yīng)的子類關(guān)系
-- 查詢父子信息:把一張表看為兩個(gè)一模一樣的表 select a.categoryname as '父欄目',b.categoryname as '子欄目' from category as a,category as b where a.categoryid = b.pid
分頁(yè)和排序
排序
-- 排序:升序 ASC 降序DESC -- 通過哪個(gè)字段排序,怎么排 select s.studentno,studentname,subjectname,studentresult from student as s inner join result as r on s.studentno = r.studentno inner join `subject` as sub on sub.subjectno=r.subjectno where subjectname = '高等數(shù)學(xué)-1' order by studentresult DESC
分頁(yè)
-- 為什么要分頁(yè) -- 緩解數(shù)據(jù)庫(kù)的壓力,給人更好的體驗(yàn) ---瀑布流 -- 分頁(yè),每頁(yè)顯示五條數(shù)據(jù) -- 語法:limit 起始值,頁(yè)面的大小 -- 網(wǎng)頁(yè)應(yīng)用:當(dāng)前頁(yè),總頁(yè)數(shù),頁(yè)面的大小 -- limit 0,5 1~5條數(shù)據(jù) -- limit 1,5 2~6條數(shù)據(jù) select * from student limit 0,5 -- 第一頁(yè) limit 0,5 (1-1)*5 -- 第二頁(yè) limit 5,5 (2-1)*5 -- 第二頁(yè) limit 10,5 (3-1)*5 -- 第N頁(yè) limit (n-1)*5 -- pageSize:頁(yè)面的大小 -- (n-1)*pageSize:起始值 -- n:當(dāng)前頁(yè) -- 數(shù)據(jù)總數(shù)/頁(yè)面大小=總頁(yè)數(shù)
語法:limit (查詢起始下標(biāo),pageSize)
子查詢
where (值是固定的,這個(gè)值是計(jì)算出來的)
本質(zhì):在where語句中嵌套一個(gè)子查詢語句
-- 查詢高等數(shù)學(xué)-1的所有考試結(jié)果(學(xué)號(hào),科目編號(hào),成績(jī)),降序排列 -- 方式1 select studentno,subjectno,studentresult from result as r inner join `subject` as sub on r.subjectno=sub.subjectno where subjectname='高等數(shù)學(xué)-1' order by studentresult DESC -- 方式2:使用子查詢 select studentno,subjectno,studentresult from result where subjectno=( select subjectno from `subject` where subjectname='高等數(shù)學(xué)-1' )order by studentresult DESC -- 查詢所有高等數(shù)學(xué)-1的學(xué)生的學(xué)號(hào) select subjectno from `subject` where subjectname='高等數(shù)學(xué)-1' -- 查詢分?jǐn)?shù)不小于80分的學(xué)生的學(xué)號(hào)和姓名 select studentno,studentname from student where studentno=( select distinct studentno from result where studentresult>=80 ) -- 查詢課程為高等數(shù)學(xué)-2并且分?jǐn)?shù)不小于80分的同學(xué)的學(xué)號(hào)和姓名 -- 聯(lián)表查詢 select s.studentno,studentname from student as s inner join result as r on s.studentno=r.studentno inner join `subject` as sub on r.subjectno=sub.subjectno where subjectname='高等數(shù)學(xué)-2' and studentresult>=80 -- 子查詢 select DISTINCT s.studentno,studentname from student as s inner join result as r on s.studentno=r.studentno where studentresult>=80 and subjectno=( select subjectno from `subject` where subjectname='高等數(shù)學(xué)-2' ) -- 子查詢:(由里及外) select studentno,studentname from student where studentno in( select studentno from result where studentresult>=80 and subjectno=( select subjectno from `subject` where subjectname='高等數(shù)學(xué)-2' ) )
到此這篇關(guān)于DQL數(shù)據(jù)查詢語句使用示例的文章就介紹到這了,更多相關(guān)DQL數(shù)據(jù)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL Innodb 存儲(chǔ)結(jié)構(gòu) 和 存儲(chǔ)Null值 用法詳解
這篇文章主要介紹了MySQL Innodb 存儲(chǔ)結(jié)構(gòu) 和 存儲(chǔ)Null值 用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Windows下修改mysql的data文件夾存放位置的方法
這篇文章主要介紹了在Windows下修改mysql的data文件夾存放位置的方法,需要的朋友可以參考下2014-03-03MySQL4 File ‘c:\mysql\share\charsets\?.conf’ not found (Errc
File ‘c:\mysql\share\charsets\?.conf’ not found (Errcode: 22) Character set ‘#33′ is not a compiled character set and is not specified in the ‘c:\mysql\share\charsets\Index’ file2013-08-08Mysql查詢時(shí)間區(qū)間日期列表實(shí)例代碼
最近常用到mysql的日期范圍搜索,下面這篇文章主要給大家介紹了關(guān)于Mysql查詢時(shí)間區(qū)間日期列表的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04MySQL按時(shí)間統(tǒng)計(jì)數(shù)據(jù)的方法總結(jié)
在本篇MYSQL的內(nèi)容里,我們給大家整理了關(guān)于按時(shí)間統(tǒng)計(jì)數(shù)據(jù)的方法內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-02-02