关于mysql 5.7 json功能

时间:2018-01-12 07:23:22

标签: mysql json

表net_card用于存储json类型的网卡信息,这是我的表:

`net_card` (
  `serial_number` varchar(100) NOT NULL,
  `data` json DEFAULT NULL,
  PRIMARY KEY (`serial_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

表net_card的一个数据是:

serial_number:12GLW02

data: {"netCard": [
{"ip": "172.16.14.67", "mac": "f8:bc:12:41:9e:a4", "bond": "no", "name": "em1", "serialNumber": "12GLW02"}, 
{"ip": "NULL", "mac": "f8:bc:12:41:9e:a5", "bond": "no", "name": "em2", "serialNumber": "12GLW02"}, 
{"ip": "NULL", "mac": "f8:bc:12:41:9e:a6", "bond": "no", "name": "em3", "serialNumber": "12GLW02"}, 
{"ip": "NULL", "mac": "f8:bc:12:41:9e:a7", "bond": "no", "name": "em4", "serialNumber": "12GLW02"}, 
{"ip": "127.0.0.1", "mac": "NULL", "bond": "no", "name": "lo", "serialNumber": "12GLW02"}]}

好吧,我想得到ipList,它的值不是" NULL",

我使用sql select data->'$[0].netCard[*].ip' from net_card limit 1来获得包含" NULL"的结果;结果是:

["172.16.14.67", "NULL", "NULL", "NULL", "127.0.0.1"]

我想得到["172.16.14.67","127.0.0.1"] 我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用字符串替换功能:

SELECT REPLACE(data->'$.netCard[*].ip', '"NULL", ', "") FROM net_card ;

但它绝对不是最好的解决方案。只有当NULL不在最后时才会有效(因为,)。