› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
fuxkcsdn
V2EX  ›  MySQL

mysql json 查询如何匹配所有数组元素的属性?

  •  
  •   fuxkcsdn · Dec 28, 2018 · 6325 views
    This topic created in 2853 days ago, the information mentioned may be changed or developed.

    mysql 版本 5.7.23

    期望的结果是查找 rsp.data.codes 数组中, object.code 值为 abc 的行

    是我的 json path 有误还是 mysql 不支持这样的查询呢?

    create table test(
      id int(1) unsigned auto_increment primary key,
      jdoc json
    )engine=InnoDB charset=utf8mb4;
    
    insert into test(jdoc) values
    ('{"req":["xyz"],"rsp":{"data":{"codes":[{"code":"abc"},{"code":"def"},{"code":"xyz"}]}}}'),
    ('{"req":["222"],"rsp":{"data":{"codes":[{"code":"ijk"},{"code":"opq"},{"code":"123"}]}}}');
    
    -- 记录为空
    select * from test where json_extract(jdoc, "$.rsp.data.codes[*].code") = "abc";
    
    -- 有记录
    select * from test where json_extract(jdoc, "$.rsp.data.codes[0].code") = "abc";
    
    fuxkcsdn
        1
    fuxkcsdn  
    OP
       Dec 28, 2018
    select * from test where JSON_CONTAINS(jdoc->"$.rsp.data.codes[*].code", JSON_ARRAY('abc'));

    or

    select * from test where JSON_CONTAINS(jdoc->"$.rsp.data.codes", JSON_OBJECT('code', 'abc'));

    v.a. https://stackoverflow.com/questions/38797627/query-a-json-column-with-an-array-of-object-in-mysql
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1203 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:37 · PVG 07:37 · LAX 16:37 · JFK 19:37
    ♥ Do have faith in what you're doing.