首页   注册   登录
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
Coding
V2EX  ›  MySQL

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

  •  
  •   0x666666 · 55 天前 · 2038 次点击
    这是一个创建于 55 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我的表结构 +-------------+--------------+------+-----+---------+-------+ | 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
        1
    0x666666   55 天前
    已经解决了,查不出数据的原因是 json 是个数组,需要用'$[0].fieldName'这种来取才能取到值,或者'$[*].fieldName'
    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   4211 人在线   最高记录 5043   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.3 · 30ms · UTC 09:15 · PVG 17:15 · LAX 01:15 · JFK 04:15
    ♥ Do have faith in what you're doing.