type exists subqueries

Reprinted: https://www.jianshu.com/p/93cd592f5c87

exists exists

exists subquery type
refers to the result of the outer sql, sql inner layer to get to test
if the inner sql established, the line fetch.



First, the background query:

A total of 15 kinds of fields to choose from mall design, as follows:

figure 1

However, the store at the time of release of goods, not necessarily release contains all fields of merchandise. May be missing a few sections, such as mobile phone accessories section, the stores do not sell. So this column will be no trade.

Figure 2, we know that you can know how many products available in the store's goods table each column. For example, cat_id = no product at column 1, cat_id = 2 column 1 product. Similarly, under section 1,6,7,12 is no product.

figure 2

Two, exists subqueries detailed explanation of the type

2.1 application scenarios:

Do 14 columns are taken out, only the following commodity columns taken out.
That is, take the table columns, the columns with names here are the goods taken out.
For example, in the store, Goods detail in FIG. 2, only the right cat_id removed. The 1,6,7 etc. cat_id not taken out.

But if the only column table, it is not apparent in this column have no product.

Question: What kind of table, called the following commodity?
A: a column provided cat_id is N, then with a select * from goods where cat_id = N ; statement data can be taken out, then there is such a commodity column.

2.2, the interpretation of the query

The first sentence

Think about the interpretation of the article at the beginning of the sentence exists: the results of sql outer layer, the inner layer to get sql to test, if the inner sql established, the row out.

Now, we begin the query:

  • The first step: the outer query category table (Fig. 1), to give equal results cat_id 1, and the inner layer to get cat_id = 1 test. At this time, the inner statement means that the product is equal to 1 cat_id removed from the product table of the goods. The results are as follows:

The result is the empty set, where exists an empty set is not true, not diverted out.

  • Step two: the outer research category table, this time to give cat_id equal to 2, then into the inner layer, corresponding to the query items equals 2 cat_id from the goods.

We found cat_id = 2, does not hold empty, inner statement, then return cat_id, cat_name.

  • The third step: one by one investigation continues, found when cat_id equal to 6,7, etc., the same does not hold, get to know the final results are as follows:

As shown above, the result would only remove the lower section of the commodity.

exists in efficiency than where and with better.

So far, three different types of sub-query is completed explain.



Author: Elsa assuming
link: https: //www.jianshu.com/p/93cd592f5c87
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin blog.csdn.net/XIAO_YAO_YOU_0/article/details/92172348