V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
0x666666
V2EX  ›  MySQL

MySQL JSON 查询的问题!求大佬解答!

  •  
  •   0x666666 · 2019-10-22 14:26:29 +08:00 · 3972 次点击
    这是一个创建于 1885 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我的表结构 +-------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+-------+ | customer_id | bigint(20) | NO | PRI | NULL | | | field_value | json | YES | | NULL | | | object_type | varchar(255) | YES | | NULL | | +-------------+--------------+------+-----+---------+-------+ 这是我存到数据库的数据,json 格式: { "customerId": 1, "objectType": "BUILDING", "fieldValue": "[{"fieldName":"客户名称","fieldType":"SINGLE_TEXT","required":true,"systemField":true},{"fieldName":"客户联系方式","fieldType":"SINGLE_TEXT","required":true,"systemField":true},{"fieldName":"客户标签","fieldType":"LABEL","required":true,"systemField":true,"values":["有钱","賊鸡儿任性","牛 B"]}]" } 我想查出 fieldValue 中的每个字段: select customer_id as customerId, object_type as objectType, json_extract(field_value, '$.fieldName') as fieldName, json_extract(field_value, '$.desc') as fieldDesc, json_extract(field_value, '$.required') as required, json_extract(field_value, '$.fieldType') as fieldType, json_extract(field_value, '$.systemField') as systemField from field where customer_id = 1 and object_type = 'BUILDING';

    然后得到一堆空的值: +------------+------------+-----------+-----------+----------+-----------+-------------+ | customerId | objectType | fieldName | fieldDesc | required | fieldType | systemField | +------------+------------+-----------+-----------+----------+-----------+-------------+ | 1 | BUILDING | NULL | NULL | NULL | NULL | NULL | +------------+------------+-----------+-----------+----------+-----------+-------------+

    1 条回复    2019-10-22 15:17:39 +08:00
    0x666666
        1
    0x666666  
    OP
       2019-10-22 15:17:39 +08:00
    已经解决了,查不出数据的原因是 json 是个数组,需要用'$[0].fieldName'这种来取才能取到值,或者'$[*].fieldName'
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5358 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 07:24 · PVG 15:24 · LAX 23:24 · JFK 02:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.