LoginSignup
10
7

More than 5 years have passed since last update.

MySQLのJSON型カラムを日本語で検索する場合

Last updated at Posted at 2017-10-19

以下のように、日本語がキーに使われているJSON型カラムを持つデータベースで、

mysql> SELECT * FROM hoge;
+----+----------------------------------------+
| id | payload                                |
+----+----------------------------------------+
|  1 | {"日本語": "にほんご"}                 |
|  2 | {"英語": "えいご"}                     |
|  3 | {"フランス語": "ふらんすご"}           |
+----+----------------------------------------+
3 rows in set (0.00 sec)

キーを指定して検索すると、エラーになる時がある。

mysql> SELECT * FROM hoge WHERE payload->'$.日本語' = 'にほんご';
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 11.

キーをクオートで括ることで解決。

mysql> SELECT * FROM hoge WHERE payload->'$."日本語"' = 'にほんご';
+----+-------------------------------+
| id | payload                       |
+----+-------------------------------+
|  1 | {"日本語": "にほんご"}        |
+----+-------------------------------+
1 row in set (0.00 sec)
10
7
0

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
10
7