在之前的表基礎上建立表
create table `price_level` (
`id` int ,
`pricelevel` int ,
`price` float ,
`description` varchar (300)
); insert into `price_level` (`id`, `pricelevel`, `price`, `description`) values('1','1','80.00','**貴的書');
insert into `price_level` (`id`, `pricelevel`, `price`, `description`) values('2','2','60.00','**適中的書');
insert into `price_level` (`id`, `pricelevel`, `price`, `description`) values('3','3','40.00','**便宜的書');
in 關鍵字的子查詢
乙個查詢語句的條件可能落在另乙個 select 語句的查詢結果中。
select * from book where booktypeid in (select id from booktype);
select * from book where booktypeid not in (select id from booktype);
帶比較運算子的子查詢
子查詢可以使用比較運算子
select * from book where price>=(select price from price_level where pricelevel=1);
帶 exists 關鍵字的子查詢
假如子查詢查詢到記錄,則進行外層查詢,否則,不執行外層查詢;
select * from book where exists (select * from booktype);
select * from book where not exists (select * from booktype);
帶 any 關鍵字的子查詢
any 關鍵字表示滿足其中任一條件
select * from book where price>= any (select price from price_level);
帶 all 關鍵字的子查詢
all 關鍵字表示滿足所有條件
select * from book where price>= all (select price from price_level);
合併查詢結果union
union 關鍵字是,資料庫系統會將所有的查詢結果合併到一起,然後去除掉相同的記錄;
select id from book union select id from price_level
union all
使用 union all,不會去除掉系統的記錄
select id from book union all select id from price_level
注意:使用union 查詢的字段個數需要一致,查詢的欄位名需要一致 mysql之子查詢
所謂子查詢,就是指在乙個查詢之中巢狀了其他的若干查詢,通過子查詢可以實現多表查詢,該查詢語句中可能包含in,any,all和exists等關鍵字,除此之外還可以包含比較運算子,子查詢經常出現在where和from字句中。where字句中的子查詢 該位置處的子查詢一般返回單行單列,多行單列,單行多列資...
MySQL 之子查詢
定義 子查詢指乙個查詢語句巢狀在另乙個查詢語句內部的查詢,這個特性從 mysql4.1 開始引入,在 select 子句中先計算子查詢,子查詢結果作為外層另乙個查詢的過濾條件,查詢可以基於乙個表或者多個表。子查詢中常用的操作符有 any some all in 和 exists。子查詢可以新增到 s...
MySQL基礎五之子查詢和連線
1.子查詢 是指出現在sql語句內的查詢 1.1.巢狀在查詢內部,而且始終被圓括號包裹 1.2.分類 使用比較符 any some all 1.3.將查詢的結構寫入資料表 insert into table name col name,select 1.4.多表之間的連線 table reperen...