首頁 > 資料庫 > mysql教程 > mysql子查詢語句是什麼

mysql子查詢語句是什麼

藏色散人
發布: 2021-12-02 11:11:29
原創
3178 人瀏覽過

mysql子查詢語句是指在另一個查詢語句中的SELECT子句,例句如「SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);」。

mysql子查詢語句是什麼

本文操作環境:Windows7系統、mysql5.7版、Dell G3電腦。

mysql子查詢語句是什麼?

mysql 子查詢語句:

1.子查詢是指在另一個查詢語句中的SELECT子句。

  例句:

  SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
登入後複製

  其中,SELECT * FROM t1 ...稱為Outer Query[外部查詢](或Outer Statement),

  SELECT column1 FROM t2 稱為Sub Query[子查詢]。

  所以,我們說子查詢是巢狀在外查詢內部。而事實上它有可能在子查詢內部再嵌套子查詢。

  子查詢必須出現在圓括號之間。

  

  行級子查詢

  SELECT * FROM t1 WHERE (col1,col2) = (SELECT col3, col4 FROM t2 WHERE id = 10);
  SELECT * FROM t1 WHERE ROW(col1,col2) = (SELECT col3, col4 FROM t2 WHERE id = 10);
登入後複製

  

  行級子查詢的回傳結果最多為一行。

  最佳化子查詢

  -- 建立資料表

CREATE TABLE IF NOT EXISTS tdb_goods(
    goods_id    SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    goods_name  VARCHAR(150) NOT NULL,
    goods_cate  VARCHAR(40)  NOT NULL,
    brand_name  VARCHAR(40)  NOT NULL,
    goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0,
    is_show     BOOLEAN NOT NULL DEFAULT 1,
    is_saleoff  BOOLEAN NOT NULL DEFAULT 0
  );
登入後複製

 -- 寫入記錄

INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('R510VC 15.6英寸笔记本','笔记本','华硕','3399',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Y400N 14.0英寸笔记本电脑','笔记本','联想','4899',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('G150TH 15.6英寸游戏本','游戏本','雷神','8499',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X550CC 15.6英寸笔记本','笔记本','华硕','2799',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X240(20ALA0EYCD) 12.5英寸超极本','超级本','联想','4999',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('U330P 13.3英寸超极本','超级本','联想','4299',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('SVP13226SCB 13.3英寸触控超极本','超级本','索尼','7999',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad mini MD531CH/A 7.9英寸平板电脑','平板电脑','苹果','1998',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)','平板电脑','苹果','3388',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)','平板电脑','苹果','2788',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('IdeaCentre C340 20英寸一体电脑 ','台式机','联想','3499',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Vostro 3800-R1206 台式电脑','台式机','戴尔','2899',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iMac ME086CH/A 21.5英寸一体电脑','台式机','苹果','9188',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )','台式机','宏碁','3699',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Z220SFF F4F06PA工作站','服务器/工作站','惠普','4288',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('PowerEdge T110 II服务器','服务器/工作站','戴尔','5388',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Mac Pro MD878CH/A 专业级台式电脑','服务器/工作站','苹果','28888',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X3250 M4机架式服务器 2583i14','服务器/工作站','IBM','6888',DEFAULT,DEFAULT);
 
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('玄龙精英版 笔记本散热器','笔记本配件','九州风神','',DEFAULT,DEFAULT);
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);
 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);
登入後複製

-- 求所有電腦產品的平均價格,且保留兩位小數,AVG,MAX,MIN、COUNT、SUM為聚合函數

SELECT ROUND(AVG(goods_price),2) AS avg_price FROM tdb_goods;
登入後複製

-- 查詢所有價格大於平均價格的商品,並且按價格降序排序

SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price > 5845.10 ORDER BY goods_price DESC;
登入後複製

  

-- 使用子查詢來實現

SELECT goods_id,goods_name,goods_price FROM tdb_goods 
  WHERE goods_price > (SELECT ROUND(AVG(goods_price),2) AS avg_price FROM tdb_goods) 
  ORDER BY goods_price DESC;
登入後複製

-- 查詢類型為「超記本」的商品價格

 

SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本';
登入後複製

-- 查詢價格大於或等於"超級本"價格的商品,並且按價格降序排列

 

SELECT goods_id,goods_name,goods_price FROM tdb_goods 
   WHERE goods_price = ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本')
   ORDER BY goods_price DESC;
登入後複製

#   

-- = ANY 或= SOME 等價於IN

SELECT goods_id,goods_name,goods_price FROM tdb_goods 
   WHERE goods_price IN (SELECT goods_price FROM tdb_goods WHERE goods_cate = '超级本')
   ORDER BY goods_price DESC;
登入後複製

-- 建立「商品分類」表

CREATE TABLE IF NOT EXISTS tdb_goods_cates(
    cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    
    cate_name VARCHAR(40)
  );
登入後複製

-- 查詢tdb_goods表的所有記錄,並且按"類別"分組

  SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
登入後複製

-- 將分組結果寫入到tdb_goods_cates資料表

 INSERT tdb_goods_cates (cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
登入後複製

-- 透過tdb_goods_cates資料表來更新tdb_goods表

UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name 
  SET goods_cate = cate_id ;
登入後複製

-- 透過CREATE...SELECT來建立資料表並且同時寫入記錄

-- SELECT brand_name FROM tdb_goods GROUP BY brand_name;
  CREATE TABLE tdb_goods_brands (
    brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    brand_name VARCHAR(40) NOT NULL
  ) SELECT brand_name FROM tdb_goods GROUP BY brand_name;
登入後複製

-- 透過tdb_goods_brands資料表來更新tdb_goods資料表(錯誤)

UPDATE tdb_goods  INNER JOIN tdb_goods_brands ON brand_name = brand_name
  SET brand_name = brand_id;
  -- Column 'brand_name' in field list is ambigous
登入後複製

  -- 正確

  UPDATE tdb_goods AS  g  INNER JOIN tdb_goods_brands AS b ON g.brand_name = b.brand_name
  SET g.brand_name = b.brand_id;
登入後複製

-- 查看tdb_goods的資料表結構

DESC tdb_goods;
登入後複製

--透過ALTER TABLE語句修改資料表結構

ALTER TABLE tdb_goods  
  CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL,
  CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
  
-- 分别在tdb_goods_cates和tdb_goods_brands表插入记录
   INSERT tdb_goods_cates(cate_name) VALUES('路由器'),('交换机'),('网卡');
   INSERT tdb_goods_brands(brand_name) VALUES('海尔'),('清华同方'),('神舟');
登入後複製

-- 在tdb_goods資料表寫入任意記錄

INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(' LaserJet Pro P1606dn 黑白激光打印机','12','4','1849');
登入後複製

-- 查詢所有商品的詳細資料(透過內連線實作)

 SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g
   INNER JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id
   INNER JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;
登入後複製

-- 查詢所有商品的詳細資訊(透過左外連線實作)

   SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g
   LEFT JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id
   LEFT JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;
登入後複製

-- 查詢所有商品的詳細資料(透過右外連線實作)

SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g
   RIGHT JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id
   RIGHT JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;
登入後複製

-- 無限分類的資料表設計

CREATE TABLE tdb_goods_types(
     type_id   SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
     type_name VARCHAR(20) NOT NULL,
     parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0
  ); 
  INSERT tdb_goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('大家电',1);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('生活电器',1);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('平板电视',3);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('空调',3);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('电风扇',4);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('饮水机',4);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑整机',2);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑配件',2);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('笔记本',9);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('超级本',9);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('游戏本',9);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('CPU',10);
  INSERT tdb_goods_types(type_name,parent_id) VALUES('主机',10);
登入後複製

-- 尋找所有分類及其父類別

  SELECT s.type_id,s.type_name,p.type_name FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS  p ON s.parent_id = p.type_id;
登入後複製

   

-- 尋找所有分類及其子類別

  SELECT p.type_id,p.type_name,s.type_name FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS  s ON s.parent_id = p.type_id;
登入後複製

-- 尋找所有分類及其子類別的數目

SELECT p.type_id,p.type_name,count(s.type_name) AS children_count FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id GROUP BY p.type_name ORDER BY p.type_id;
登入後複製

-- 為tdb_goods_types新增child_count欄位

ALTER TABLE tdb_goods_types ADD child_count MEDIUMINT UNSIGNED NOT NULL DEFAULT 0;
登入後複製

-- 將剛才查詢到的子類別數量更新至tdb_goods_types資料表

UPDATE tdb_goods_types AS t1 INNER JOIN ( SELECT p.type_id,p.type_name,count(s.type_name) AS children_count FROM tdb_goods_types AS p 
                                            LEFT JOIN tdb_goods_types AS s ON s.parent_id = p.type_id 
                             
                                            GROUP BY p.type_name 
                                            ORDER BY p.type_id ) AS t2 
  ON  t1.type_id = t2.type_id 
  SET t1.child_count = t2.children_count;
登入後複製

-- 複製編號為12,20的兩筆記錄

SELECT * FROM tdb_goods WHERE goods_id IN (19,20);
登入後複製

-- INSERT ... SELECT實作複製

INSERT tdb_goods(goods_name,cate_id,brand_id) SELECT goods_name,cate_id,brand_id FROM tdb_goods WHERE goods_id IN (19,20);
登入後複製

-- 尋找重複記錄

SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2;
登入後複製

-- 刪除重複記錄

  DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM tdb_goods GROUP BY goods_name HAVING count(goods_name) >= 2 ) AS t2  ON t1.goods_name = t2.goods_name  WHERE t1.goods_id > t2.goods_id;
登入後複製

【相關推薦:mysql影片教學

以上是mysql子查詢語句是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板