简体   繁体   English

如何解析 MySQL 版本 < 5.7 中的 json 列

[英]How to parse json column in MySQL Version < 5.7

I have a column called inventory .我有一个名为inventory的列。

id ..... inventory

the content of this column is:本栏目内容为:

{"STC1":{"count":"1"},"STC2":{"count":0}}

the count value is variable. count数值是可变的。 I don't want to use application side, I want to use sql.我不想使用应用程序端,我想使用sql。

for example例如

what I want:我想要的是:

... where STC1.count > 0

or或者

... where STC1.count > 1 or STC2.count < 5

You can't, according to the official MySQL website , the JSON support has been introduced for the 5.7.8 version, it isn't available natively for older versions.你不能,根据官方 MySQL 网站,5.7.8 版本已经引入了 JSON 支持,它不适用于旧版本。

For MySQL < 5.7.8, this JSON content is just a string, MySQL has nothing to extract structured data from it.对于 MySQL < 5.7.8,此 JSON 内容只是一个字符串,MySQL 无法从中提取结构化数据。

At best you'll be abble to check WHERE inventory LIKE '%"STC1":{"count":"0"%' to detect the row with a STC1 at 0 or something like that but not much more, and it will rapidly becoming really hairy to do anything complex.充其量你将能够检查 WHEREinventory WHERE inventory LIKE '%"STC1":{"count":"0"%'以检测STC10或类似但不多的行,它会很快做任何复杂的事情都变得非常毛茸茸。

In general it's better to store atomic data to avoid this kind of problem.一般来说,最好存储原子数据以避免这种问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM