mysql json数据类型搜索数组内的值

时间:2017-07-29 00:10:45

标签: mysql arrays json

我在MySQL表中有一个json类型的列,该列中的值是一个简单的1D数组(例如[1,2,3,4,5])。如何使用json_contains函数返回包含值" 5"的所有行。在json数组?

1 个答案:

答案 0 :(得分:0)

尝试:

PlayerInteractEvent

请参阅db-fiddle

<强>更新

mysql> DROP TABLE IF EXISTS `tbl`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl` (
    ->   `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->   `json` JSON NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `tbl`
    ->   (`json`)
    -> VALUES
    ->   ('[1,2,3,4,5]'),
    ->   ('[1,2,3,4]'), 
    ->   ('[1,2]'),
    ->   ('[1]'),
    ->   ('[1,5]');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT
    ->   `id`,
    ->   `json`
    -> FROM
    ->   `tbl`
    -> WHERE
    ->   JSON_CONTAINS(`json`, '5');
+----+-----------------+
| id | json            |
+----+-----------------+
|  1 | [1, 2, 3, 4, 5] |
|  5 | [1, 5]          |
+----+-----------------+
2 rows in set (0.00 sec)

请参阅db-fiddle