Rechercher dans le manuel MySQL

13.2.14 VALUES Statement

VALUES is a DML statement introduced in MySQL 8.0.19 which returns a set of one or more rows as a table. In other words, it is a table value constructor which also functions as a standalone SQL statement.

  1. VALUES row_constructor_list [ORDER BY column_designator] [LIMIT BY number]
  2.  
  3. row_constructor_list:
  4.     ROW(value_list)[, ROW(value_list)][, ...]
  5.  
  6. value_list:
  7.     value[, value][, ...]    
  8.  
  9. column_designator:
  10.     column_index

The VALUES statement consists of the VALUES keyword followed by a list of one or more row constructors, separated by commas. A row constructor consists of the ROW() row constructor function with a value list of one or more scalar values as its argument. A value can be a literal of any MySQL data type or an expression that resolves to a scalar value.

ROW() cannot be empty (but can be NULL). Each ROW() in the same VALUES statement must have the same number of values in its value list.

The DEFAULT keyword is not supported by VALUES and causes a syntax error, except when it is used to supply values in an INSERT statement.

The output of VALUES is a table:

  1. mysql> VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8);
  2. +----------+----------+----------+
  3. | column_0 | column_1 | column_2 |
  4. +----------+----------+----------+
  5. |        1 |       -2 |        3 |
  6. |        5 |        7 |        9 |
  7. |        4 |        6 |        8 |
  8. +----------+----------+----------+
  9. 3 rows in set (0.00 sec)

The columns of the table output from VALUES have the implicitly named columns column_0, column_1, column_2, and so on, always beginning with 0. This fact can be used to order the rows by column using an optional ORDER BY clause in the same way that this clause works with a SELECT statement, as shown here:

  1. mysql> VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8) ORDER BY column_1;
  2. +----------+----------+----------+
  3. | column_0 | column_1 | column_2 |
  4. +----------+----------+----------+
  5. |        1 |       -2 |        3 |
  6. |        4 |        6 |        8 |
  7. |        5 |        7 |        9 |
  8. +----------+----------+----------+
  9. 3 rows in set (0.00 sec)

The VALUES statement also supports a LIMIT clause for limiting the number of rows in the output.

The VALUES statement is permissive regarding data types of column values; you can mix types within the same column, as shown here:

  1. mysql> VALUES ROW("q", 42, '2019-12-18'),
  2.     ->     ROW(23, "abc", 98.6),
  3.     ->     ROW(27.0002, "Mary Smith", '{"a": 10, "b": 25}');
  4. +----------+------------+--------------------+
  5. | column_0 | column_1   | column_2           |
  6. +----------+------------+--------------------+
  7. | q        | 42         | 2019-12-18         |
  8. | 23       | abc        | 98.6               |
  9. | 27.0002  | Mary Smith | {"a": 10, "b": 25} |
  10. +----------+------------+--------------------+
  11. 3 rows in set (0.00 sec)
Important

VALUES with one or more instances of ROW() acts as a table value constructor; although it can be used to supply values in an INSERT or REPLACE statement, do not confuse it with the VALUES keyword that is also used for this purpose. You should also not confuse it with the VALUES() function that refers to column values in INSERT ... ON DUPLICATE KEY UPDATE.

In addition, do not confuse VALUES ROW() with the ROW() form of the row constructor (see Section 13.2.11.5, “Row Subqueries”).

VALUES can be used in many cases where you could employ SELECT, including those listed here:

  • With UNION, as shown here:

    1. mysql> SELECT 1,2 UNION SELECT 10,15;
    2. +----+----+
    3. | 1  | 2  |
    4. +----+----+
    5. |  1 |  2 |
    6. | 10 | 15 |
    7. +----+----+
    8. 2 rows in set (0.00 sec)
    9.  
    10. mysql> VALUES ROW(1,2) UNION VALUES ROW(10,15);
    11. +----------+----------+
    12. | column_0 | column_1 |
    13. +----------+----------+
    14. |        1 |        2 |
    15. |       10 |       15 |
    16. +----------+----------+
    17. 2 rows in set (0.00 sec)

    It is also possible in this fashion to union together constructed tables having more than one row, like this:

    1. mysql> VALUES ROW(1,2),ROW(3,4),ROW(5,6) UNION VALUES ROW(10,15),ROW(20,25);
    2. +----------+----------+
    3. | column_0 | column_1 |
    4. +----------+----------+
    5. |        1 |        2 |
    6. |        3 |        4 |
    7. |        5 |        6 |
    8. |       10 |       15 |
    9. |       20 |       25 |
    10. +----------+----------+
    11. 5 rows in set (0.00 sec)

    VALUES can also be used in unions with SELECT statements, TABLE statements, or both.

    The constructed tables in the UNION must contain the same number of columns, just as if you were using SELECT. See Section 13.2.10.3, “UNION Clause”, for further examples.

  • In joins. See Section 13.2.10.2, “JOIN Clause”, for more information and examples.

  • In place of VALUES() in an INSERT or REPLACE statement, in which case its semantics differ slightly from what is described here. See Section 13.2.6, “INSERT Statement”, for details.

  • In place of the source table in CREATE TABLE ... SELECT and CREATE VIEW ... SELECT. See the descriptions of these statements for more information and examples.


Rechercher dans le manuel MySQL

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-values.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

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

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.

Table des matières Haut