mariadb Mysql在Json数组中查找短语

tquggr8v  于 2023-02-16  发布在  Mysql
关注(0)|答案(1)|浏览(138)

我正在寻找一种方法来查找行的json表的给定元素匹配的模式。
让我们从mysql表开始:

CREATE TABLE `person` (
  `attributes` json DEFAULT NULL
);

INSERT INTO `person` (`attributes`)
VALUES ('[{"scores": 1, "name": "John"},{"scores": 1, "name": "Adam"}]');

INSERT INTO `person` (`attributes`)
VALUES ('[{"scores": 1, "name": "Johny"}]');

INSERT INTO `person` (`attributes`)
VALUES ('[{"scores": 1, "name": "Peter"}]');

如何找到属性[*].name包含John*模式的所有记录?
John*的情况下,查询应该返回2条记录(包括John和Johny)。

czfnxgou

czfnxgou1#

SELECT DISTINCT person.*
FROM person
CROSS JOIN JSON_TABLE(person.attributes, '$[*]' COLUMNS (name TEXT PATH '$.name')) parsed
WHERE parsed.name LIKE 'John%';

https://sqlize.online/sql/mysql80/c9e4a3ffa159c4be8c761d696e06d946/

相关问题