Rechercher dans le manuel MySQL
13.2.11.8 Derived Tables
This section discusses general characteristics of derived
tables. For information about lateral derived tables preceded by
the LATERAL
keyword, see
Section 13.2.11.9, “Lateral Derived Tables”.
A derived table is an expression that generates a table within
the scope of a query FROM
clause. For
example, a subquery in a SELECT
statement FROM
clause is a derived table:
The JSON_TABLE()
function
generates a table and provides another way to create a derived
table:
The [AS]
clause is mandatory because every table in a
tbl_name
FROM
clause must have a name. Any columns in
the derived table must have unique names. Alternatively,
tbl_name
may be followed by a
parenthesized list of names for the derived table columns:
The number of column names must be the same as the number of table columns.
For the sake of illustration, assume that you have this table:
Here is how to use a subquery in the FROM
clause, using the example table:
Result:
+------+------+------+
| sb1 | sb2 | sb3 |
+------+------+------+
| 2 | 2 | 4 |
+------+------+------+
Here is another example: Suppose that you want to know the average of a set of sums for a grouped table. This does not work:
However, this query provides the desired information:
Notice that the column name used within the subquery
(sum_column1
) is recognized in the outer
query.
The column names for a derived table come from its select list:
- +---+---+---+---+
- | 1 | 2 | 3 | 4 |
- +---+---+---+---+
- | 1 | 2 | 3 | 4 |
- +---+---+---+---+
To provide column names explicitly, follow the derived table name with a parenthesized list of column names:
- +---+---+---+---+
- | a | b | c | d |
- +---+---+---+---+
- | 1 | 2 | 3 | 4 |
- +---+---+---+---+
A derived table can return a scalar, column, row, or table.
Derived tables are subject to these restrictions:
A derived table cannot be a correlated subquery.
A derived table cannot contain references to other tables of the same
SELECT
.Prior to MySQL 8.0.14, a derived table cannot contain outer references. This is a MySQL restriction that is lifted in MySQL 8.0.14, not a restriction of the SQL standard. For example, the derived table
dt
in the following query contains a referencet1.b
to the tablet1
in the outer query:- FROM t2
The query is valid in MySQL 8.0.14 and higher. Before 8.0.14, it produces an error:
Unknown column 't1.b' in 'where clause'
The optimizer determines information about derived tables in
such a way that EXPLAIN
does not
need to materialize them. See
Section 8.2.2.4, “Optimizing Derived Tables, View References, and Common Table Expressions
with Merging or Materialization”.
It is possible under certain circumstances that using
EXPLAIN
SELECT
will modify table data. This can occur if the
outer query accesses any tables and an inner query invokes a
stored function that changes one or more rows of a table.
Suppose that there are two tables t1
and
t2
in database d1
, and a
stored function f1
that modifies
t2
, created as shown here:
Referencing the function directly in an
EXPLAIN
SELECT
has no effect on t2
, as
shown here:
This is because the SELECT
statement did not reference any tables, as can be seen in the
table
and Extra
columns of
the output. This is also true of the following nested
SELECT
:
- *************************** 1. row ***************************
- id: 1
- select_type: PRIMARY
- possible_keys: NULL
- key_len: NULL
- ref: NULL
- rows: NULL
- filtered: NULL
- +-------+------+------------------------------------------+
- | Level | Code | Message |
- +-------+------+------------------------------------------+
- +-------+------+------------------------------------------+
However, if the outer SELECT
references any tables, the optimizer executes the statement in
the subquery as well, with the result that t2
is modified:
- *************************** 1. row ***************************
- id: 1
- select_type: PRIMARY
- partitions: NULL
- type: system
- possible_keys: NULL
- key_len: NULL
- ref: NULL
- rows: 1
- filtered: 100.00
- Extra: NULL
- *************************** 2. row ***************************
- id: 1
- select_type: PRIMARY
- table: a1
- partitions: NULL
- possible_keys: NULL
- key_len: NULL
- ref: NULL
- rows: 1
- filtered: 100.00
- Extra: NULL
- *************************** 3. row ***************************
- id: 2
- select_type: DERIVED
- partitions: NULL
- possible_keys: NULL
- key_len: NULL
- ref: NULL
- rows: NULL
- filtered: NULL
- +------+
- | c1 |
- +------+
- | 5 |
- +------+
This also means that an
EXPLAIN
SELECT
statement such as the one shown here may take a
long time to execute because the
BENCHMARK()
function is executed
once for each row in t1
:
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-derived-tables.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.