基于SQL中的數(shù)據(jù)查詢語句匯總
where條件表達式
--統(tǒng)計函數(shù)
Select count(1) from student;
--like模糊查詢
--統(tǒng)計班上姓張的人數(shù)
select count(*) from student where realName like '張%';
--統(tǒng)計班上張姓兩個字的人數(shù)
select count(*) from student where realName like '張_';
--統(tǒng)計班上杭州籍的學生人數(shù)
select count(*) from student where home like '%杭州%';
--查詢班上每位學生的年齡
select realName,year(now())-year(birthday) as age from student;
--查詢90年出生的學生
select realName from student where year(birthday)>='1990';
--查詢1987-1990年出生的學生
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型血的學生
select realName,blood from student where blood in('B','O');
子查詢
子查詢也可稱之為嵌套查詢,有些時候,一次查詢不能解決問題,需要多次查詢。
按子查詢返回的記錄行數(shù)區(qū)分,可分為單行子查詢和多行子查詢;
select * from emp where sal>( select sal from emp where ename='ALLEN‘ or ename =‘KING')
上例是找出比allen工資高的所有員工
A.子查詢一般先于主語句的運行
B.必須有( ),表示一個整體
C.習慣上把子查詢放在條件的右邊
多行子查詢:some,any,all
連接語句(應用于多表查詢)
包括:內聯(lián),外聯(lián)(左外連和右外聯(lián))
內聯(lián)(inner join):把兩張表相匹配的行查詢出來。
--查詢每個學生的各科成績,顯示“姓名”“課程名”“分數(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
相關文章
MySQL導出數(shù)據(jù)遇到secure-file-priv問題的解決方法
這篇文章主要為大家詳細介紹了MySQL導出數(shù)據(jù)遇到secure-file-priv問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
mysql創(chuàng)建函數(shù)出現(xiàn)1418錯誤的解決辦法
本篇文章是對在Mysql中創(chuàng)建函數(shù)報“ERROR 1418”的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
解析mysql二進制日志處理事務與非事務性語句的區(qū)別
本篇文章是對mysql二進制日志處理事務與非事務性語句的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06
MySQL: mysql is not running but lock exists 的解決方法
下面可以參考下面的方法步驟解決。最后查到一個網(wǎng)友說可能和log文件有關,于是將log文件給移除了,再重啟MySQL終于OK了2009-06-06

