我在列extra_fields
中有一个带有JSON数组的数据库。我需要通过id提取一个值。
JSON看起来像这样,尽管每行的对象数量是随机的:
[
{"id":"1","value":"48768"},
{"id":"2","value" ["String","http:someurl"]},
{"id":"5","value":"somevalue"},
{"id":"6","value":""},
{"id":"8","value":"Op-Ed"},
{"id":"9","value":"8111,13498,15408"},
{"id":"10","value":"30"},
{"id":"11","value":"This is the target string"}
]
我可以使用以下方法提取一组id:
SELECT extra_fields->>"$[*].id" FROM esqt7_k2_items;
我可以使用以下方法提取值数组:
SELECT extra_fields->>"$[*].value" FROM esqt7_k2_items;
我可以在第n个零序对象位置提取单个值:
SELECT extra_fields->>"$[2].value" FROM esqt7_k2_items;
但问题是存在可变数量的对象,我特别需要object id = 11的值.JSON_EXTRACT似乎不支持过滤,因为JSONPATH的每个变体似乎都失败了。 $..[?(@.id=11)].value
作为JSONPATH工作,但是这个(以及许多变体)都失败了:
SELECT extra_fields->"$.[?(@.id=11)].value" FROM esqt7_k2_items; #FAILS
这是一个迁移项目,JSON的东西就是Joomla选择实现额外字段的方式。如果你问我,有点痛。
答案 0 :(得分:1)
您可以考虑一个选项(小心性能问题):
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `esqt7_k2_items`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `esqt7_k2_items` (
-> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `extra_fields` JSON NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `esqt7_k2_items`
-> (`extra_fields`)
-> VALUES
-> ('
'> [
'> {"id": "1", "value": "48768"},
'> {"id": "2", "value": ["String","http:someurl"]},
'> {"id": "5", "value": "somevalue"},
'> {"id": "6", "value": ""},
'> {"id": "8", "value": "Op-Ed"},
'> {"id": "9", "value": "8111,13498,15408"},
'> {"id": "10", "value": "30"},
'> {"id": "11", "value": "This is the target string"}
'> ]
'> '),
-> ('
'> [
'> {"id": "1", "value": ""},
'> {"id": "9", "value": "ONE This is the target string"}
'> ]
'> '),
-> ('
'> [
'> {"id": "6", "value": ""},
'> {"id": "11", "value": "TWO This is the target string"}
'> ]
'> ');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SET @`search_id` := '11';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT `der`.`id`, `der`.`value`
-> FROM (
-> SELECT
-> `id`,
-> JSON_UNQUOTE(
-> JSON_EXTRACT(`extra_fields`,
-> CONCAT((SELECT
-> JSON_UNQUOTE(
-> JSON_SEARCH(`extra_fields` ->> '$[*].id', 'one', @`search_id`)
-> )
-> FROM `esqt7_k2_items` `esqt7_k2_items_in`
-> WHERE `esqt7_k2_items_out`.`id` = `esqt7_k2_items_in`.`id`
-> ), ".value"
-> )
-> )
-> ) `value`
-> FROM `esqt7_k2_items` `esqt7_k2_items_out`
-> ) `der`
-> WHERE `der`.`value` IS NOT NULL;
+----+-------------------------------+
| id | value |
+----+-------------------------------+
| 1 | This is the target string |
| 3 | TWO This is the target string |
+----+-------------------------------+
2 rows in set (0.00 sec)
请参阅db-fiddle。
答案 1 :(得分:1)
我发现最简单的方式
对象id = 11
的值
通过找到正确的"索引"所需对象,做一点 PHP 。 首先,我们通过简单的请求获取所有对象ID:
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Clone the request to add the new header.
const clonedRequest = req.clone();
clonedRequest.headers.set("ModuleId", this.dnnContext.moduleId);
clonedRequest.headers.set("TabId", this.dnnContext.tabId);
// Pass on the cloned request instead of the original request.
return next.handle(clonedRequest);
}
然后在PHP中
> SELECT extra_fields->>"$[*].id" FROM esqt7_k2_items;
然后通过传递 $ rightIndex :
来获得正确的对象$rightIndex = array_search($ids, '11');
简单如同地狱!您可以在其他任何地方执行相同的操作,例如,当您需要更新数组中的对象或其值/值等时。 那就是干杯!