首頁 資料庫 mysql教程 MySQL学习足迹记录12--使用子查询_MySQL

MySQL学习足迹记录12--使用子查询_MySQL

Jun 01, 2016 pm 01:31 PM
mysql 記錄

bitsCN.com

MySQL学习足迹记录12--使用子查询

 

1.子查询(subquery):即嵌套在其他查询中的查询

原始数据如下:

 

   mysql> SELECT order_num FROM orderitems;+-----------+| order_num |+-----------+|     20005 ||     20005 ||     20009 ||     20005 ||     20009 ||     20008 ||     20006 ||     20009 ||     20009 ||     20005 ||     20007 |+-----------+11 rows in set (0.01 sec)mysql> SELECT cust_id FROM orders;+---------+| cust_id |+---------+|   10001 ||   10001 ||   10003 ||   10004 ||   10005 |+---------+5 rows in set (0.01 sec)现在先分步查询step1:  mysql> SELECT order_num          -> FROM orderitems          -> WHERE prod_id = 'TNT2';+-----------+| order_num |+-----------+|     20005 ||     20007 |+-----------+2 rows in set (0.00 sec)step2: mysql> SELECT cust_id FROM orders          -> WHERE order_num IN( 20005,20007);+---------+| cust_id |+---------+|   10001 ||   10004 |+---------+2 rows in set (0.00 sec) Step3:  使用子查询把step1,step2组合起来(即把20005,20007换掉)  mysql> SELECT cust_id           -> FROM orders           -> WHERE order_num IN( SELECT order_num           ->                                        FROM orderitems           ->                                         WHERE prod_id = 'TNT2');+---------+| cust_id |+---------+|   10001 ||   10004 |+---------+2 rows in set (0.00 sec)TIPS:  在SELECT语句中,子查询总是从内向外处理的。  子查询可以嵌套多重 step4:  mysql> SELECT cust_name,cust_contact           -> FROM customers           -> WHERE cust_id IN (10001,10004);      #(10001,10004)既是step3查询的结果+----------------+--------------+| cust_name      | cust_contact |+----------------+--------------+| Coyote Inc.    | Y Lee        || Yosemite Place | Y Sam        |+----------------+--------------+2 rows in set (0.01 sec)step5:把step4的IN (10001,10004)换成子查询 mysql> SELECT cust_name,cust_contact          -> FROM customers         -> WHERE cust_id IN (SELECT cust_id         ->                                   FROM orders         ->                                   WHERE order_num IN (SELECT order_num         ->                                                                           FROM orderitems         ->                                                                           WHERE prod_id = 'TNT2'));+----------------+--------------+| cust_name      | cust_contact |+----------------+--------------+| Coyote Inc.    | Y Lee        || Yosemite Place | Y Sam        |+----------------+--------------+2 rows in set (0.00 sec)
登入後複製

2.计算字段使用子查询

原始数据

   mysql> SELECT cust_id FROM orders;+---------+| cust_id |+---------+|   10001 ||   10001 ||   10003 ||   10004 ||   10005 |+---------+5 rows in set (0.01 sec)mysql> SELECT cust_id FROM customers;+---------+| cust_id |+---------+|   10001 ||   10002 ||   10003 ||   10004 ||   10005 |+---------+5 rows in set (0.00 sec)mysql> SELECT cust_id,(SELECT COUNT(*) FROM orders         ->                                WHERE orders.cust_id = customers.cust_id) AS orders        -> FROM customers       -> ORDER BY cust_id;+---------+--------+              | cust_id | orders |+---------+--------+|   10001 |      2 ||   10002 |      0 ||   10003 |      1 ||   10004 |      1 ||   10005 |      1 |+---------+--------+5 rows in set (0.00 sec)
登入後複製

 

 

TIPS:

  子查询最常见的使用是在WHERE子句的IN操作符中,以及用来填充计算列

bitsCN.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP 的大數據結構處理技巧 PHP 的大數據結構處理技巧 May 08, 2024 am 10:24 AM

PHP 的大數據結構處理技巧

如何優化 PHP 中的 MySQL 查詢效能? 如何優化 PHP 中的 MySQL 查詢效能? Jun 03, 2024 pm 08:11 PM

如何優化 PHP 中的 MySQL 查詢效能?

如何在 PHP 中使用 MySQL 備份和還原? 如何在 PHP 中使用 MySQL 備份和還原? Jun 03, 2024 pm 12:19 PM

如何在 PHP 中使用 MySQL 備份和還原?

如何使用 PHP 插入資料到 MySQL 表? 如何使用 PHP 插入資料到 MySQL 表? Jun 02, 2024 pm 02:26 PM

如何使用 PHP 插入資料到 MySQL 表?

Java 枚舉類型在資料庫中的應用場景有哪些? Java 枚舉類型在資料庫中的應用場景有哪些? May 05, 2024 am 09:06 AM

Java 枚舉類型在資料庫中的應用場景有哪些?

如何修復 MySQL 8.4 上的 mysql_native_password 未載入錯誤 如何修復 MySQL 8.4 上的 mysql_native_password 未載入錯誤 Dec 09, 2024 am 11:42 AM

如何修復 MySQL 8.4 上的 mysql_native_password 未載入錯誤

如何在 PHP 中使用 MySQL 預存程序? 如何在 PHP 中使用 MySQL 預存程序? Jun 02, 2024 pm 02:13 PM

如何在 PHP 中使用 MySQL 預存程序?

如何使用 PHP 建立 MySQL 表? 如何使用 PHP 建立 MySQL 表? Jun 04, 2024 pm 01:57 PM

如何使用 PHP 建立 MySQL 表?

See all articles