【问题标题】:Mysql join on json value issueMysql加入json值问题
【发布时间】:2021-08-14 18:17:12
【问题描述】:

我在 mysql 5.7 服务器上有以下表格

produit__parure

id_parure (int) ids_produits_parure (json)
21 ["34809", "34823", "34813"]
22 ["35703", "35854", "35877"]

produit

id_product (int) ... other columns
34809 ...
34810 ...

我尝试使用此查询将 id.prod = value on 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 NULL NULL

我做错了什么? 结果我想要的是:

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"]

【问题讨论】:

    标签: mysql mysql-json


    【解决方案1】:

    您是否可能仅在 JSON_CONTAINS 上将 ID 选择为 JSON?我不知道生成的代码应该是什么样子,除了可能类似的东西:

    JSON_CONTAINS(pp.ids_produits_parure->'$[*]', CAST(pp as JSON))
    

    因为我怀疑ID是一个数组,你不会搜索pp而不是p吗?

    参考: https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#:~:text=JSON_CONTAINS%20%28target%2C%20candidate%20%5B%2C%20path%20%5D%29%20Indicates%20by,found%20at%20a%20specific%20path%20within%20the%20target.

    【讨论】:

    • 我尝试了 Dan Chase 的答案,但它产生了错误的结果(我的 ids_produits_parure 不等于 id_prod ) id_prod id_parure ids_produits_parure 34809 21 ["34809", "34823", "34813"] 34809 22 [ “35703”、“35854”、“35877”]
    • 我认为我们需要对数据集是什么以及您要完成的工作进行更多定义,以便我们可以建议适当的查询结构。你的意图改变了上面的一切。我的回答仅概述了它具有 NULL 的原因,这就是您所要求的,但我无法预测它应该返回什么。您添加的 cmets 在我们进行过程中增加了清晰度,但我们需要它。
    【解决方案2】:

    你的 KSON_CONTAINS 有点。

    Json 在大多数情况下难以处理,因此您应该考虑切换到规范化设计

    CREATE TABLE produit__parure
        (`id_parure` int, `ids_produits_parure` json)
    ;
        
    INSERT INTO produit__parure
        (`id_parure`, `ids_produits_parure`)
    VALUES
        ('21', '["34809", "34823", "34813"]'),
        ('22', '["35703", "35854", "35877"]')
    ;
    
    CREATE TABLE produit
        (`id_product` int, `other columns` varchar(3))
    ;
        
    INSERT INTO produit
        (`id_product`, `other columns`)
    VALUES
        (34809, '...'),
        (34810, '...')
    

    ;

    SELECT JSON_CONTAINS(`ids_produits_parure`, CONCAT('"','34809' ,'"'), '$') FROM produit__parure
    
    | JSON_CONTAINS(`ids_produits_parure`, CONCAT('"','34809' ,'"'), '$') | | -------------------------------------------------- ----------------: | | 1 | | 0 |
    SELECT p.id_product, pp.* FROM produit p left JOIN produit__parure pp on
    JSON_CONTAINS(pp.ids_produits_parure, CONCAT('"',p.id_product,'"'),'$')
    where id_product=34809
    
    id_product | id_parure | ids_produits_parure ---------: | --------: | :------------------------------------------------- ---- 34809 | 21 | 5b223334383039222c20223334383233222c20223334383133225d

    db小提琴here

    【讨论】:

    • 感谢您的帮助,但结果很奇怪,为什么我们有:5b223334383039222c20223334383233222c20223334383133225d 而不是正确的 ids_products_parure 值?
    • 它在我的服务器上运行良好,我得到了正确的结果,再次感谢您
    • 这只是对网站的解释,在您的服务器和工作台上它工作得很好,也请不要忘记[接受答案]8https://stackoverflow.com/help/someone -答案)
    猜你喜欢
    • 2011-04-03
    • 2011-10-14
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多