Keine Cache-Version

Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code DEF204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.

Rechercher dans le manuel MySQL

12.3.4 Assignment Operators

Table 12.5 Assignment Operators

Name Description
= Assign a value (as part of a SET statement, or as part of the SET clause in an UPDATE statement)
:= Assign a value

  • :=

    Assignment operator. Causes the user variable on the left hand side of the operator to take on the value to its right. The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). You can perform multiple assignments in the same SET statement. You can perform multiple assignments in the same statement.

    Unlike =, the := operator is never interpreted as a comparison operator. This means you can use := in any valid SQL statement (not just in SET statements) to assign a value to a variable.

    1. mysql> SELECT @var1, @var2;
    2.         -> NULL, NULL
    3. mysql> SELECT @var1 := 1, @var2;
    4.         -> 1, NULL
    5. mysql> SELECT @var1, @var2;
    6.         -> 1, NULL
    7. mysql> SELECT @var1, @var2 := @var1;
    8.         -> 1, 1
    9. mysql> SELECT @var1, @var2;
    10.         -> 1, 1
    11.  
    12. mysql> SELECT @var1:=COUNT(*) FROM t1;
    13.         -> 4
    14. mysql> SELECT @var1;
    15.         -> 4

    You can make value assignments using := in other statements besides SELECT, such as UPDATE, as shown here:

    1. mysql> SELECT @var1;
    2.         -> 4
    3. mysql> SELECT * FROM t1;
    4.         -> 1, 3, 5, 7
    5.  
    6. mysql> UPDATE t1 SET c1 = 2 WHERE c1 = @var1:= 1;
    7. Query OK, 1 row affected (0.00 sec)
    8. Rows matched: 1  Changed: 1  Warnings: 0
    9.  
    10. mysql> SELECT @var1;
    11.         -> 1
    12. mysql> SELECT * FROM t1;
    13.         -> 2, 3, 5, 7

    While it is also possible both to set and to read the value of the same variable in a single SQL statement using the := operator, this is not recommended. Section 9.4, “User-Defined Variables”, explains why you should avoid doing this.

  • =

    This operator is used to perform value assignments in two cases, described in the next two paragraphs.

    Within a SET statement, = is treated as an assignment operator that causes the user variable on the left hand side of the operator to take on the value to its right. (In other words, when used in a SET statement, = is treated identically to :=.) The value on the right hand side may be a literal value, another variable storing a value, or any legal expression that yields a scalar value, including the result of a query (provided that this value is a scalar value). You can perform multiple assignments in the same SET statement.

    In the SET clause of an UPDATE statement, = also acts as an assignment operator; in this case, however, it causes the column named on the left hand side of the operator to assume the value given to the right, provided any WHERE conditions that are part of the UPDATE are met. You can make multiple assignments in the same SET clause of an UPDATE statement.

    In any other context, = is treated as a comparison operator.

    1. mysql> SELECT @var1, @var2;
    2.         -> NULL, NULL
    3. mysql> SELECT @var1 := 1, @var2;
    4.         -> 1, NULL
    5. mysql> SELECT @var1, @var2;
    6.         -> 1, NULL
    7. mysql> SELECT @var1, @var2 := @var1;
    8.         -> 1, 1
    9. mysql> SELECT @var1, @var2;
    10.         -> 1, 1

    For more information, see Section 13.7.5.1, “SET Syntax for Variable Assignment”, Section 13.2.12, “UPDATE Syntax”, and Section 13.2.11, “Subquery Syntax”.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-assignment-operators.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut