27
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

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)

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

27
17
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?