Help us understand the problem. What is going on with this article?

MySQLのUPDATEで複数のカラムを更新するとき、カンマではなくANDと書いてしまったら

More than 5 years have passed since last update.

こんなかんじでテーブルを用意して、

mysql> CREATE TABLE foo (id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, a INT(10), b INT(10), PRIMARY KEY (id));
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO foo (a,b) VALUES (10,100);
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM foo;
+----+------+------+
| id | a    | b    |
+----+------+------+
|  1 |   10 |  100 |
+----+------+------+
1 row in set (0.00 sec)

UPDATE分を打つ。a=11とb=101の間はカンマが正しいのだけど、ANDと書いてしまった。

mysql> UPDATE foo SET a=11 AND b=101 WHERE id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM foo;
+----+------+------+
| id | a    | b    |
+----+------+------+
|  1 |    0 |  100 |
+----+------+------+
1 row in set (0.00 sec)

更新されたことになってるのになぜか更新されてない。

シンタックスエラーにもならない。何が起こってるんだろうか。

mysql> UPDATE foo SET a=11, b=101 WHERE id=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM foo;
+----+------+------+
| id | a    | b    |
+----+------+------+
|  1 |   11 |  101 |
+----+------+------+
1 row in set (0.00 sec)

もちろんこれだと更新される。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away