小燕子,哈哈哈哈~~~~~~~~~~
相關子查詢是指引用了外部查詢列的子查詢,即子查詢會對外部查詢的每行進行一次計算。
舉個例子
root:test> show create table test1\G *************************** 1. row *************************** Table: test1 Create Table: CREATE TABLE `test1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `course` varchar(20) DEFAULT NULL, `score` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 insert into test1(name,course,score) values ('張三','語文',80), ('李四','語文',90), ('王五','語文',93), ('張三','數學',77), ('李四','數學',68), ('王五','數學',99), ('張三','英語',90), ('李四','英語',50), ('王五','英語',89); root:test> select * from test1; +----+--------+--------+-------+ | id | name | course | score | +----+--------+--------+-------+ | 1 | 張三 | 語文 | 80 | | 2 | 李四 | 語文 | 90 | | 3 | 王五 | 語文 | 93 | | 4 | 張三 | 數學 | 77 | | 5 | 李四 | 數學 | 68 | | 6 | 王五 | 數學 | 99 | | 7 | 張三 | 英語 | 90 | | 8 | 李四 | 英語 | 50 | | 9 | 王五 | 英語 | 89 | +----+--------+--------+-------+
使用相關子查詢
root:test> select * -> from test1 a -> where 2>(select count(*) from test1 where course=a.course and score>a.score) -> order by a.course,a.score desc; +----+--------+--------+-------+ | id | name | course | score | +----+--------+--------+-------+ | 6 | 王五 | 數學 | 99 | | 4 | 張三 | 數學 | 77 | | 7 | 張三 | 英語 | 90 | | 9 | 王五 | 英語 | 89 | | 3 | 王五 | 語文 | 93 | | 2 | 李四 | 語文 | 90 | +----+--------+--------+-------+ rows in set (0.01 sec)
分析下這個sql:
select * from test1 a where 2 > (select count(*) from test1 where course=a.course and score>a.score)
相關子查詢的特點就是子查詢依賴與外部查詢,在這里面其實是 select * from test 已經先執行了一遍了,查出了所有的數據
然后相關子查詢針對每一行數據進行select count(*) from test1 where course=a.course and score>a.score
例如:
第一行是張三,數學77,那么相關子查詢做的工作就是找出test表所有課程是數學的行,查詢 張三,77|李四,68|王五,99
然后where條件score>77,查詢出王五,99,count=1,這時候外部條件2>1,符合。
第二行是李四,數學68,那么相關子查詢做的工作就是找出test表所有課程是數學的行,查詢 張三,77|李四,68|王五,99
然后where條件score>68,查詢出張三,77,王五,99,count=2,這時候外部條件2>2,不符合。
第三行是王五,數學99,那么相關子查詢做的工作就是找出test表所有課程是數學的行,查詢 張三,77|李四,68|王五,99
然后where條件score>99,沒有數據,這時候外部條件2>0,符合。
那么就篩選出了數學最大的2個人,張三和王五。
其實這里的子查詢就是找出和當前行類型能匹配上的比他大的有多少,沒有比他大的他就是最大
那么找top1就是 1>(xxx),topN就是N>(xxxxx)
