Mysql基礎入門(五)子查詢

2021-10-09 00:25:25 字數 2043 閱讀 1102

有如下表,現要求編寫sql語句,檢視年齡比「李斯文」小的學生,要求顯示這些學生的資訊 ,紅框中的學生是比李斯文年齡小的學生。

第一步:查詢得到「李斯文」的出生日期

select borndate from student 

where studentname=

"李斯文";-

>

1993-07

-03

第二步:利用where語句,篩選出生日期比「李斯文」大的學生

select

*from student

where borndate >

'1993-07-03'

;

select

*from student

where borndate >

(select borndate from student

where studentname=

"李斯文"

);

select name,age from person 

where age >

(select age from person

where name =

'孫權'

)

輸出結果為

'魏國'

)輸出結果為

some在sql中的邏輯運算符號,如果在一系列比較中,有些值為true,那麼結果就為true。

select name from person 

where countryid =

some

--用等號和以下查詢到的值比較,如果與其中乙個相等,就返回

(select countryid from country

where countryname =

'魏國'

)

all是sql中的邏輯運算子好,如果一系列的比較都為true,那麼結果才能為true。

select name from person 

where countryid >

all--當countryid大於以下返回的所有id,此結果才為true,此結果才返回

(select countryid from country

where countryname =

'魏國'

)

結果

exists是sql中的邏輯運算符號。如果子查詢有結果集返回,那麼就為true。exists代表「存在」的意義,它只查詢滿足條件的那些記錄。一旦找到第乙個匹配的記錄後,就馬上停止查詢。

但是如果子查詢中因為加了條件而沒有結果集返回,則主語句就不執行了:

select

*from person

where

exists

(select

*from person

where person_id =

100--如果不存在person_id的記錄,則子查詢沒有結果集返回,主語句不執行

)

參考:

MySQL查詢語句(五) 子查詢

mysql查詢語句 四 order by與limit 把查詢語句的結果當where的判斷條件進行查詢,子查詢的結果為單列。1 查出本 最新的 goods id最大 的一條商品 使用排序 按照goods id desc排序,再取第一行 mysql select goods id,goods name ...

下五子棋的bot 五子棋演算法

include include include include include include jsoncpp json.h c 編譯時預設包含此庫 define n 7 每個節點的分支數 以下為各棋型的識別碼 權重 define win 1 4000 define lose 2 4000 defi...

C語言入門 仿五子棋遊戲

仿五子棋遊戲 define crt secure no warnings define row 15 define col 15 include include include 1.定義二維陣列表示棋盤 char g chess board row col int input row 1 int i...