简体   繁体   English

使用 Laminas-Db 在 WHERE 子句中使用 JSON_EXTRACT

[英]Using JSON_EXTRACT in WHERE clause using Laminas-Db

I have a mysql table with a json datatype used to store json_encoded data:我有一个 mysql 表,其中包含用于存储 json_encoded 数据的 json 数据类型:

id|author_id|date_inserted|comments
---------------------------------------------
1|32|2022-04-01|{"id": "343","some":"data"}
2|15|2022-02-15|{"id": "24","some":"data"}
3|24|2022-05-22|{"id": "995","some":"data"}

Using laminas-db, how is it possible to use JSON_EXTRACT in a where clause?使用 laminas-db,如何在 where 子句中使用 JSON_EXTRACT? For instance, when using a tablegateway, i would like to update the json data of a row using something like the following:例如,当使用 tablegateway 时,我想使用如下内容更新一行的 json 数据:

$this->tableGateway->update($data, [JSON_EXTRACT('comments', '$.id') = 24]);

I was able to achieve it with the following:我能够通过以下方式实现它:

 $commentId = 24;
 $where = new Where();
 $where->addPredicate(new Expression('JSON_EXTRACT(comments, "$.id") = ?', $commentId));
 return $this->tableGateway->update($data, $where);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM