Thoroughly understand mysql subqueries

According to different values, it can be divided into

1. Scalar quantum query

Only one value

2. Column query

One column (one field), multiple values

3. Row subquery (table subquery)

One row and multiple columns/Multiple rows and multiple columns

note:

1. Use single-line operators >, >=, <, <=, =, etc. before standard sub-query

2. Multi-value sub-queries (column sub-query, row sub-query) use the multi-row operator
in/not in
any/some
all

According to the relationship with the outer query, it can be divided into

1. Independent sub-queries
are executed only once and will not affect the efficiency of the outer query

2. Relying on sub-queries (prohibited in the actual development process)
will be queried every time where or having is executed, wasting memory and time.

Alternative plan: Use independent subquery to read the data found out by dependent subquery to generate a new temporary table, and use this temporary table to join and merge with the original table.

The sub-query rules at each location
can only be followed by sub-query (one value)
after select, and any sub-query can be followed by from as a temporary table
. The rules after where and having are consistent. Any subquery can be accessed. Pay attention to the symbol.

Note: It is recommended to use subqueries only after from.

Guess you like

Origin blog.csdn.net/m0_45311187/article/details/112986930