简体   繁体   English

在Mysql 5.6中查询json列表

[英]Query on a list of json in Mysql 5.6

I am using mysql 5.6 and it will not be feasible for me to upgrade it to 5.7. 我正在使用mysql 5.6,将其升级到5.7将不可行。 I have a table which stores json as an attribute. 我有一个表,将json作为属性存储。 Attaching screenshot for reference. 随附屏幕截图以供参考。

在此,policy_status列包含每个用户的不同策略的状态和值作为json。

Here, the column policy_status contains status and values of different policies as json for each user. 在此,policy_status列包含每个用户的不同策略的状态和值作为json。 How can I find the list of users, say, with appVersion' status as success and value = 1437. I got a few references online but as I am new to stored procedures I am not able to reach a solution. 我怎样才能找到用户列表,例如,appVersion的状态为成功,值=1437。我在网上获得了一些参考,但是由于我不熟悉存储过程,因此无法找到解决方案。 I will appreciate any help. 我将不胜感激。 Thanks in advance. 提前致谢。

It is not efficient at all but may can help you with ideas: 它根本没有效率,但是可以帮助您提出建议:

SELECT *
FROM data
WHERE
  (LOCATE('"employmentType":["status":"success"]', policy_status) > 0
   AND
   LOCATE('"value": 1', policy_status) > 0);

Using the LOCATE function you can see whether the field contains your desired appVersion and value strings. 使用LOCATE函数,您可以查看该字段是否包含所需的appVersionvalue字符串。 See sqlfiddle demo here. 参见sqlfiddle演示

Where the simple test data : 其中简单的测试数据

CREATE TABLE data (
  id INT UNSIGNED NOT NULL,
  policy_status TEXT
);

INSERT INTO data (id, policy_status) VALUES
(1,'{"employmentType":["status":"success"], "value": 1}'),
(2,'{"employmentType":["status":"no"], "value": 1}'),
(3,'{"employmentType":["status":"no"], "value": 0}'),
(4,'{"employmentType":["status":"success"], "value": 0}'),
(5,'{"employmentType":["status":"no"], "value": 1}');

gives the result : 给出结果

{"employmentType":["status":"success"], "value": 1}

Where both strings are found. 找到两个字符串的地方。

UPDATE : 更新

Also if you can add FULLTEXT index for your policy_status column than you can use fulltext search in the WHERE clause: 另外,如果您可以为policy_status列添加FULLTEXT索引,则可以在WHERE子句中使用全文本搜索:

...
WHERE
   MATCH (policy_status) AGAINST ('+"employmentType:[status:success]" +"value: 1"' IN BOOLEAN MODE)

Note the + and " characters in AGAINST(...) . They are special boolean full-text search operators. See here . 注意AGAINST(...)+"字符。它们是特殊的布尔型全文本搜索运算符。 请参见此处

A leading or trailing plus sign indicates that this word must be present in each row that is returned 前导或尾随加号表示此单词必须出现在返回的每一行中

and

A phrase that is enclosed within double quote (") characters matches only rows that contain the phrase literally, as it was typed. The full-text engine splits the phrase into words and performs a search in the FULLTEXT index for the words. Nonword characters need not be matched exactly. 包含在双引号(“)字符中的短语仅与按字面意义包含该短语的行匹配(如键入的那样)。全文引擎将短语拆分成单词,并在FULLTEXT索引中搜索这些单词。非单词字符不需要完全匹配。

If it is not an option in your case, you can use LIKE for matching the substrings: 如果您不是这种情况,则可以使用LIKE来匹配子字符串:

...
WHERE
  (policy_status LIKE '%"employmentType":["status":"success"]%'
   AND
   policy_status LIKE '%"value": 1%');

See sqlfiddle demo for both. 两者均参见sqlfiddle演示

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

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