2

假设我有这样的查询

SELECT DISTINCT customer_name 
FROM borrower 
WHERE customer_name in (SELECT customer_name FROM depositor)

我们可以将上面视为两个查询

一个

SELECT DISTINCT customer_name 
FROM borrower 
WHERE customer_name

SELECT customer_name 
FROM depositor

哪个先执行?这条规则对所有子查询都一样吗?

我们可以编写哪些类型的子查询?给我一些指导以开始使用教程链接。

4

1 回答 1

0

编辑为第一个答案是错误的。但解释是正确的:|

A先执行。当子查询相互关联时,应用上面介绍的“所有”子查询。

+----+--------------------+-----------+----------------+---------------+---------------+---------+------+------+----------+--------------------------+
| id | select_type        | table     | type           | possible_keys | key           | key_len | ref  | rows | filtered | Extra                    |
+----+--------------------+-----------+----------------+---------------+---------------+---------+------+------+----------+--------------------------+
|  1 | PRIMARY            | borrower  | index          | NULL          | customer_name | 52      | NULL |    6 |   100.00 | Using where; Using index |
|  2 | DEPENDENT SUBQUERY | depositor | index_subquery | customer_name | customer_name | 52      | func |    1 |   100.00 | Using index              |
+----+--------------------+-----------+----------------+---------------+---------------+---------+------+------+----------+--------------------------+

顺便提一句。MySQL 6 将消除这个问题,并且PRIMARY两个查询的选择类型都将变为。见1

于 2012-09-22T11:23:48.207 回答