基于SQL中的數(shù)據(jù)查詢語句匯總
where條件表達(dá)式
--統(tǒng)計(jì)函數(shù)
Select count(1) from student;
--like模糊查詢
--統(tǒng)計(jì)班上姓張的人數(shù)
select count(*) from student where realName like '張%';
--統(tǒng)計(jì)班上張姓兩個字的人數(shù)
select count(*) from student where realName like '張_';
--統(tǒng)計(jì)班上杭州籍的學(xué)生人數(shù)
select count(*) from student where home like '%杭州%';
--查詢班上每位學(xué)生的年齡
select realName,year(now())-year(birthday) as age from student;
--查詢90年出生的學(xué)生
select realName from student where year(birthday)>='1990';
--查詢1987-1990年出生的學(xué)生
select realName from student where year(birthday)<='1990' and year(birthday)>='1987';
select * from student where year(birthday) between '1987' and '1990';
--查詢班上男女生人數(shù)
select sex,count(*) from student group by sex;
--in子句查詢班上B或O型血的學(xué)生
select realName,blood from student where blood in('B','O');
子查詢
子查詢也可稱之為嵌套查詢,有些時(shí)候,一次查詢不能解決問題,需要多次查詢。
按子查詢返回的記錄行數(shù)區(qū)分,可分為單行子查詢和多行子查詢;
select * from emp where sal>( select sal from emp where ename='ALLEN‘ or ename =‘KING')
上例是找出比allen工資高的所有員工
A.子查詢一般先于主語句的運(yùn)行
B.必須有( ),表示一個整體
C.習(xí)慣上把子查詢放在條件的右邊
多行子查詢:some,any,all
連接語句(應(yīng)用于多表查詢)
包括:內(nèi)聯(lián),外聯(lián)(左外連和右外聯(lián))
內(nèi)聯(lián)(inner join):把兩張表相匹配的行查詢出來。
--查詢每個學(xué)生的各科成績,顯示“姓名”“課程名”“分?jǐn)?shù)”三列
select a.realname,c.courseName,b.score from stu_student as a inner join stu_score as b on a.sid=b.sid inner join stu_course c on b.cid=c.cid
還有一種方法,不采用inner join:
select a.realname,c.courseName,b.score from student a,score b,course c where a.sid=b.sid and c.cid=b.cid
外聯(lián)分左外聯(lián)和右外聯(lián):
Left outer join:查詢兩邊表的匹配記錄,且將左表的不匹配記錄也查詢出來。
Right outer join:等上,將右表不匹配記錄也查詢出來。
select a.realname,b.score from stu_student as a left outer join stu_score as b on a.sid=b.sid
相關(guān)文章
MySQL中LIKE?BINARY和LIKE模糊查詢實(shí)例代碼
通常在實(shí)際應(yīng)用中,會涉及到模糊查詢的需求,下面這篇文章主要給大家介紹了關(guān)于MySQL中LIKE?BINARY和LIKE模糊查詢的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Mysql數(shù)據(jù)庫從5.6.28版本升到8.0.11版本部署項(xiàng)目時(shí)遇到的問題及解決方法
這篇文章主要介紹了Mysql數(shù)據(jù)庫從5.6.28版本升到8.0.11版本過程中遇到的問題及解決方法,解決辦法有三種,每種方法給大家介紹的都很詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05MySQL如何基于Explain關(guān)鍵字優(yōu)化索引功能
這篇文章主要介紹了MySQL如何基于Explain關(guān)鍵字優(yōu)化索引功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10