欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SQL中的數(shù)據(jù)查詢語(yǔ)句匯總

 更新時(shí)間:2013年07月31日 08:50:45   作者:  
以下是對(duì)SQL中的數(shù)據(jù)查詢語(yǔ)句進(jìn)行了匯總介紹,需要的朋友可以過(guò)來(lái)參考下

where條件表達(dá)式
--統(tǒng)計(jì)函數(shù)

復(fù)制代碼 代碼如下:

Select count(1) from student;

--like模糊查詢
--統(tǒng)計(jì)班上姓張的人數(shù)
復(fù)制代碼 代碼如下:

select count(*) from student where realName like '張%';

--統(tǒng)計(jì)班上張姓兩個(gè)字的人數(shù)
復(fù)制代碼 代碼如下:

select count(*) from student where realName like '張_';

--統(tǒng)計(jì)班上杭州籍的學(xué)生人數(shù)
復(fù)制代碼 代碼如下:

select count(*) from student where home like '%杭州%';

--查詢班上每位學(xué)生的年齡
復(fù)制代碼 代碼如下:

select realName,year(now())-year(birthday) as age from student;

--查詢90年出生的學(xué)生
復(fù)制代碼 代碼如下:

select realName from student where year(birthday)>='1990';

--查詢1987-1990年出生的學(xué)生
復(fù)制代碼 代碼如下:

select realName from student where year(birthday)<='1990' and year(birthday)>='1987';
select * from student where year(birthday) between '1987' and '1990';

--查詢班上男女生人數(shù)
復(fù)制代碼 代碼如下:

select sex,count(*) from student group by sex;

--in子句查詢班上B或O型血的學(xué)生
復(fù)制代碼 代碼如下:

select realName,blood from student where blood in('B','O'); 

子查詢
子查詢也可稱之為嵌套查詢,有些時(shí)候,一次查詢不能解決問(wèn)題,需要多次查詢。

按子查詢返回的記錄行數(shù)區(qū)分,可分為單行子查詢和多行子查詢;

復(fù)制代碼 代碼如下:

select * from emp where sal>(       select sal from emp where ename='ALLEN‘ or ename =‘KING')

上例是找出比allen工資高的所有員工

A.子查詢一般先于主語(yǔ)句的運(yùn)行
B.必須有( ),表示一個(gè)整體
C.習(xí)慣上把子查詢放在條件的右邊
多行子查詢:some,any,all

連接語(yǔ)句(應(yīng)用于多表查詢)
包括:內(nèi)聯(lián),外聯(lián)(左外連和右外聯(lián))
內(nèi)聯(lián)(inner join):把兩張表相匹配的行查詢出來(lái)。

--查詢每個(gè)學(xué)生的各科成績(jī),顯示“姓名”“課程名”“分?jǐn)?shù)”三列

復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

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:查詢兩邊表的匹配記錄,且將左表的不匹配記錄也查詢出來(lái)。
Right outer join:等上,將右表不匹配記錄也查詢出來(lái)。
復(fù)制代碼 代碼如下:

select a.realname,b.score from stu_student as a left outer join stu_score as b on a.sid=b.sid

相關(guān)文章

最新評(píng)論