mysql - Mysql加入json值问题
问题描述
我在 mysql 5.7 服务器上有以下表格
produit__parure
id_parure (int) | ids_produits_parure (json) |
---|---|
21 | [“34809”、“34823”、“34813”] |
22 | [“35703”、“35854”、“35877”] |
和produit
:
id_product (int) | ...其他列 |
---|---|
34809 | ... |
34810 | ... |
我尝试使用以下查询在 id.prod = value 上加入 ids_produits_parure :
SELECT p.id_prod, pp.* FROM produit p left JOIN produit__parure pp on
JSON_CONTAINS(pp.ids_produits_parure->'$[*]', CAST(p.id_prod as JSON))
where id_prod=34809
但它在 produit__parure 文件上返回 NULL:
id_prod | id_parure | ids_produits_parure |
---|---|---|
34809 | 无效的 | 无效的 |
我究竟做错了什么?结果我想要的是:
id_prod | id_parure | ids_produits_parure |
---|---|---|
34809 | 21 | [“34809”、“34823”、“34813”] |
我尝试了 Dan Chase 的答案,但它产生了错误的结果(我的行 ids_produits_parure 不等于 id_prod )
id_prod | id_parure | ids_produits_parure |
---|---|---|
34809 | 21 | [“34809”、“34823”、“34813”] |
34809 | 22 | [“35703”、“35854”、“35877”] |
解决方案
您是否可能仅在 JSON_CONTAINS 上选择 ID 作为 JSON?我不知道生成的代码应该是什么样子,除了可能类似的东西:
JSON_CONTAINS(pp.ids_produits_parure->'$[*]', CAST(pp as JSON))
因为我怀疑ID是一个数组,你不会搜索pp而不是p吗?
推荐阅读
- gcloud - 有没有更简单的方法来列出组织中**不是**的 GCP 项目?
- unity3d - 检测丢失的服务器连接
- sql - 基于同一张表结果的 SQL
- javascript - 使用 Firebase 查询参数“in”时,出现“操作员”错误
- performance - Dart 性能:数千个低级 IO 操作的最佳方法
- ajax - 如何在 laravel ajax 中使整行 yajra 数据表可点击?那是每一行都可以点击。以下是我的示例代码
- react-native - React native:如何选择具有 nativeID 的视图元素
- javascript - Next.js 动态路由错误:提供的路径 `page-name` 与页面不匹配:`/[slug]`
- python - 列表理解引发 RuntimeError
- java - 数字程序输出错误结果的平均值