MySQL自連接與子查詢方式
1. 自連接
自連接是表自身與自身做笛卡爾積,在SQL中進行條件查詢,都是指定某一列或多個列之間進行關(guān)系運算,無法進行行與行之間的運算,在某些情況下需要對行與行之間進行關(guān)系運算,就要使用到自連接。
自連接的本質(zhì)是將行轉(zhuǎn)為列;
示例:顯示所有“課程id為3”比“課程id為1”成績高的成績信息:
(成績信息在score表中)
(1)對score進行自連接(別名求笛卡爾積)并刪除無效信息:
mysql> select* from score as s1, score as s2 where s1.student_id = s2.student_id;
(2)選出第一列id=1的課程與第二列id=3的課程:
mysql> select* from score as s1, score as s2 -> where s1.student_id = s2.student_id -> and s1.course_id = 1 -> and s2.course_id = 3;
(該結(jié)果表示有三個同學(xué)同時選修了這兩門課程)
(3)增加左列成績小于右列成績條件,SQL指令與查詢結(jié)果為:
mysql> select* from score as s1,score as s2 -> where s1.student_id = s2.student_id -> and s1.course_id = 1 -> and s2.course_id = 3 -> and s1.score < s2.score; +-------+------------+-----------+-------+------------+-----------+ | score | student_id | course_id | score | student_id | course_id | +-------+------------+-----------+-------+------------+-----------+ | 70.5 | 1 | 1 | 98.5 | 1 | 3 | | 33.0 | 3 | 1 | 68.0 | 3 | 3 | +-------+------------+-----------+-------+------------+-----------+ 2 rows in set (0.00 sec)
注:
(1)不能直接進行自連接:
mysql> select* from score,score; ERROR 1066 (42000): Not unique table/alias: 'score'
需要為表指定兩個別名,即:
mysql> select* from score as s1, score as s2;
2. 子查詢(嵌套查詢)
子查詢是指嵌入其他SQL語句中的select語句,即將多個查詢語句合并為一個語句;
2.1 子查詢分類
(1)單行子查詢:查詢結(jié)果只有一條記錄;
(2)多行子查詢:查詢結(jié)果為多條記錄;
2.2 單行子查詢示例1:查詢不想畢業(yè)同學(xué)的同班同學(xué)
(1)分步查詢SQL指令及查詢結(jié)果為:
mysql> select classes_id from student where name="不想畢業(yè)"; +------------+ | classes_id | +------------+ | 1 | +------------+ 1 row in set (0.00 sec) mysql> select name from student where classes_id =1; +------------+ | name | +------------+ | 黑旋風(fēng)李逵 | | 菩提老祖 | | 白素貞 | | 許仙 | | 不想畢業(yè) | +------------+ 5 rows in set (0.00 sec)
(2)子查詢SQL指令及查詢結(jié)果為:
mysql> select name from student where classes_id = (select classes_id from student where name="不想畢業(yè)"); +------------+ | name | +------------+ | 黑旋風(fēng)李逵 | | 菩提老祖 | | 白素貞 | | 許仙 | | 不想畢業(yè) | +------------+ 5 rows in set (0.00 sec)
即將條件查詢的某一個值替換為一個select查詢語句;
2.3 多行子查詢示例2:查詢語文或英語課程的信息成績
先查詢出兩個課程的課程id,再根據(jù)course_id在score表中查詢;
(1)分步查詢SQL指令及查詢結(jié)果為:
mysql> select id from course where name="語文" or name="英文"; +----+ | id | +----+ | 4 | | 6 | +----+ 2 rows in set (0.00 sec) mysql> select* from score where course_id in(4,6); +-------+------------+-----------+ | score | student_id | course_id | +-------+------------+-----------+ | 98.0 | 1 | 6 | | 72.0 | 4 | 6 | | 43.0 | 6 | 4 | | 79.0 | 6 | 6 | | 92.0 | 7 | 6 | +-------+------------+-----------+ 5 rows in set (0.00 sec)
(2)子查詢SQL指令及查詢結(jié)果為:
mysql> select* from score where course_id in(select id from course where name="語文" or name="英文"); +-------+------------+-----------+ | score | student_id | course_id | +-------+------------+-----------+ | 98.0 | 1 | 6 | | 72.0 | 4 | 6 | | 43.0 | 6 | 4 | | 79.0 | 6 | 6 | | 92.0 | 7 | 6 | +-------+------------+-----------+ 5 rows in set (0.00 sec)
3. 合并查詢
合并查詢就是將兩個查詢語句的結(jié)果合并到一起;
3.1 示例1:查詢id=3或者名字為英文的課程
(1)使用邏輯或?qū)崿F(xiàn)查詢:
mysql> select* from course where id<3 or name="英文"; +----+--------------+ | id | name | +----+--------------+ | 1 | Java | | 2 | 中國傳統(tǒng)文化 | | 6 | 英文 | +----+--------------+ 3 rows in set (0.00 sec)
(2)使用union關(guān)鍵字進行合并查詢:
mysql> select* from course where id<3 union select* from course where name="英文"; +----+--------------+ | id | name | +----+--------------+ | 1 | Java | | 2 | 中國傳統(tǒng)文化 | | 6 | 英文 | +----+--------------+ 3 rows in set (0.00 sec)
注:
(1)union與邏輯或的區(qū)別:
邏輯或只能對一張表的查詢結(jié)果進行合并,但union可以對多張表的查詢結(jié)果進行合并(要求多個結(jié)果的列須對應(yīng));
(2)union與union all的區(qū)別:
使用union關(guān)鍵字對多個查詢結(jié)果進行合并時會自動去重,但unionall不會去重;
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
centos 7系統(tǒng)下編譯安裝 mysql5.7教程
因為Mysql5.7的更新特性還是非常多,所以這篇文章就給大家介紹以下在centos上面編譯安裝mysql5.7的教程。本文給大家介紹的步驟還是相對來說比較詳細(xì)的,相信對大家具有一定的參考借鑒價值,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-11-11SQL中from_unixtime函數(shù)的使用方法實例
在MySQL數(shù)據(jù)表設(shè)計中,時間字段一般都設(shè)計為時間戳格式的,開發(fā)人員去查看的時候就顯得有點不方便,可以使用FROM_UNIXTIME轉(zhuǎn)換成日期格式進行查看,下面這篇文章主要給大家介紹了關(guān)于SQL中from_unixtime函數(shù)的使用方法的相關(guān)資料,需要的朋友可以參考下2022-08-08