这是我的表结构 +-------------+--------------+------+-----+---------+-------+ | 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
0x666666 OP 已经解决了,查不出数据的原因是 json 是个数组,需要用'$[0].fieldName'这种来取才能取到值,或者'$[*].fieldName'
|