MySQL 子查詢 練習題

2021-09-28 15:08:45 字數 3816 閱讀 2848

測試資料表:

suppliers 表

s_id  s_name          s_city     s_zip   s_call  

----

----

----

----

----

----

-------

----

----

----

101 fastfruit inc. tianjin 300000

48075

102lt supplies chongqing 400000

44333

103acme shanghai 200000

90046

104fnk inc. zhongshan 528437

11111

105 good set taiyuang 030000

22222

106 just eat ours beijing 010

45678

107dk inc. zhengzhou 450000

33332

fruits 表

f_id      s_id  f_name      f_price  

----

----

----

----

----

----

-------

a2 103 apricot 2.20

b1 101 blackberry 10.20

b2 104 berry 7.60

b5 107 ***x 3.60

bs1 102 orange 11.20

bs2 105 melon 8.20

c0 101 cherry 3.20

l2 104 lemon 6.40

m1 106 mango 15.60

m2 105 xbabay 2.60

m3 105 xxtt 11.60

o2 103 coconut 9.20

t1 102 banana 10.30

t2 102 grape 5.30

t4 107 xbababa 3.60

第一條語句,不太懂,到底是返回了查詢結果的資訊表,還是 ture

(推測點:select * from suppliers where fruits.s_id= suppliers.s_idand fruits.s_id=107` 語句中,fruits與suppliers形成了 內連線)

select * from fruits

where exists

(select * from suppliers

where fruits.`s_id` = suppliers.`s_id` and fruits.s_id=

107)

;

f_id      s_id  f_name   f_price  

----

----

----

-------

----

----

-b5 107 ***x 3.60

t4 107 xbababa 3.60

下面為 兩個表內連線 的查詢結果 :

select

*from suppliers ,fruits

where fruits.

`s_id`

= suppliers.

`s_id`

and fruits.s_id=

107

s_id  s_name   s_city     s_zip   s_call  f_id      s_id  f_name   f_price  

----

----

-------

-------

----

----

----

----

----

----

-------

----

----

-107

dk inc.

zhengzhou

450000

33332 b5 107 ***x 3.60

107dk inc.

zhengzhou

450000

33332 t4 107 xbababa 3.60

第二條語句,查詢之後返回ture,然後查詢出了 fruits表的全部資料

(推測點: select * from suppliers where s_id=107 語句中 suppliers表與上層的select中 fruits沒有連線條件)

select *

from fruits

where exists

(select *

from suppliers where s_id=

107)

;

f_id      s_id  f_name      f_price  

----

----

----

----

----

----

-------

a2 103 apricot 2.20

b1 101 blackberry 10.20

b2 104 berry 7.60

b5 107 ***x 3.60

bs1 102 orange 11.20

bs2 105 melon 8.20

c0 101 cherry 3.20

l2 104 lemon 6.40

m1 106 mango 15.60

m2 105 xbabay 2.60

m3 105 xxtt 11.60

o2 103 coconut 9.20

t1 102 banana 10.30

t2 102 grape 5.30

t4 107 xbababa 3.60

MySQL查詢練習題

在挑戰實驗1中構建的成績管理系統中,物理老師想要找出分數最高的同學進行表揚,請你找出這個同學並把他的資訊 id 姓名 性別 輸出到路徑 tmp 下的 physics.txt檔案中。同時 tom 的化學成績有異議,需要在原來的基礎上加3分,請更新 tom 的化學成績。wget資料庫 gradesyst...

(1 4 3 1)查詢練習題

一 選擇題 1.若查詢每個記錄的概率均等,則在具有n個記錄的連續順序檔案中採用順序查詢法查詢乙個記錄,其平均查詢長度asl為 c a n 1 2 b.n 2 c.n 1 2 d.n 2.對n個元素的表做順序查詢時,若查詢每個元素的概率相同,則平均查詢長度為 a a n 1 2 b.n 2 c.n d...

多表查詢練習題

部門表 create table dept id int primary key primary key,部門id dname varchar 50 部門名稱 loc varchar 50 部門所在地 新增4個部門 insert into dept id,dname,loc values 10,教學...