定義兩個表tb1和tb2
create table tbl1 ( num1 int not null);
create table tbl2 ( num2 int not null);
向兩個表中插入資料:
insert into tbl1 values(1), (5), (13), (27);
insert into tbl2 values(6), (14), (11), (20);
anysome關鍵字的子查詢
select num1
from tbl1
where num1 > any (select num2 from tbl2);
all關鍵字的子查詢
select num1
from tbl1
where num1 > all (select num2 from tbl2);
exists關鍵字的子查詢
select * from fruits
where exists
(select s_name from suppliers where s_id = 107);
select * from fruits
where f_price>10.20 and exists
(select s_name from suppliers where s_id = 107);
select * from fruits
where not exists
(select s_name from suppliers where s_id = 107);
帶in關鍵字的子查詢
select c_id
from orders
where o_num in (select o_num from orderitems where f_id = 'c0');
select c_id
from orders
where o_num not in (select o_num from orderitems where f程式設計客棧_id = 'c0');
帶比較運算子的子查詢
select s_id, f_name from fruits
where s_id =
(select s1.s_id from suppliers as s1 where s1.s_city = 'tianjin');
<>所有非
select s_id, f_name from fruits
where s_id <>
(select s1.s_id from suppliers as s1 where s1.s_city = 'tianjin');
定義兩個表tb1和tb2
create table tbl1 ( num1 int not null);
create table tbl2 ( num2 int not null);
向兩個表中插入資料
insert into tbl1 values(1), (5), (13), (27);
insert into tbl2 values(6), (14), (11), (20);
【例.53】返回tbl2表的所有 num2 列,然後將 tbl1 中的 num1 的值與之進行比較,只要大於 num2的任何值為符合查詢條件的結果
select num1
from tbl1
where num1 > any (select num2 from tbl2);
【例.54】返回tbl1表的中比tbl2表num2 列所有值都大的值
select num1
from tbl1
where num1 > all (select num2 from tbl2);
【例.55】查詢表suppliers表中是否存在s_id=107的**商,如果存在則查詢fruits表中的記錄
select * from fruits
where exists
(select s_name from suppliers where s_id = 107);
【例.56】查詢表suppliers表中是否存在s_id=sbdngzgpl107的**商,如果存在則查詢fruits表中的f_price大於10.20的記錄
select * from fruits
where f_price>10.20 and exists
(select s_name from suppliers where s_id = 107);
【例.57】查詢表suppliers表中是否存在s_id=107的**商,如果不存在則查詢fruits表中的記錄
select * from fruits
where not exists
(select s_name from suppliers where s_id = 107);
【例.58】在orderitems表中查詢訂購f_id為c0sbdngzgpl的訂單號,並根據訂單號查詢具有訂單號的客戶c_id
select c_id from orders where o_num in
(select o_num from orderitems where f_id = 'c0');
【例.59】與前乙個例子語句類似,但是在select語句中使用not in操作符
select c_id from orders where o_num n in
(select o_num from orderitems where f_id = 'c0');
【例.60】在suppliers表中查詢s_city等於tianjin的**商s_id,然後在fruits表中查詢所有該**商提供的水果的種類
select s_id, f_name from fruits
where s_id =
(select s1.s_id from suppliers as s1 where s1.s_city = 'tianjin');
【例.61】在suppliers表中查詢s_city等於tianjin的**商s_id,然後在fruits表中查詢所有非該**商提供的水果的種類,sql語句如下:
select s_id, f_name from fruits
where s_id <>
(select s1.s_id from supplisbdngzgplers as s1 where s1.s_city = 'tianjin');
Mysql子查詢例項
一,子選擇基本用法 1,子選擇的定義 子迭擇允許把乙個查詢巢狀在另乙個查詢當中。比如說 乙個考試記分專案把考試事件分為考試 t 和測驗 q 兩種情形。下面這個查詢就能只找出學生們的考試成績 select from score where event id in select event id fro...
mysql子查詢教程 MySQL子查詢詳解
子查詢指乙個查詢語句巢狀在另乙個查詢語句內部的查詢,這個特性從 mysql 4.1 開始引入,在 select 子句中先計算子查詢,子查詢結果作為外層另乙個查詢的過濾條件,查詢可以基於乙個表或者多個表。子查詢中常用的操作符有 any some all in 和 exists。子查詢可以新增到 sel...
MySQL連線查詢例項詳解
建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...