简体   繁体   English

SQL json 提取数组中的位置

[英]SQL json extract where in array

I have users table with json type attributes column:我有带有json类型attributes列的users表:

Example value:示例值:

attributes: {"connection": "HTTP"},
attributes: {"connection": "API"}

When I tried get using WHERE IN() with one param all works correctly:当我尝试使用带有一个参数的WHERE IN()时,一切正常

SELECT * FROM users WHERE JSON_EXTRACT(attributes, '$.connection') IN ("HTTP");

But when I tried with multiple value it's doesn't work for me:但是当我尝试使用多个值时,它对我不起作用

SELECT * FROM users WHERE JSON_EXTRACT(attributes, '$.connection') IN ("API", "HTTP");

In this case return nothing.在这种情况下,什么也不返回。 How I can get users which has one of defined connections inside WHERE IN() values?如何获取在WHERE IN()值中具有已定义连接之一的用户?

In MySQL 5.7 (possibly only later 5.7 versions; I saw some indications that some JSON_EXTRACT behavior changed in 5.7.10 or 5.7.11), it does not appear to cast the JSON value returned by JSON_EXTRACT to a string when used as the left operand to IN .在 MySQL 5.7 中(可能只有更高的 5.7 版本;我看到一些迹象表明某些 JSON_EXTRACT 行为在 5.7.10 或 5.7.11 中发生了变化),当 JSON_EXTRACT 返回的左操作数用作字符串时,它似乎没有将 JSON 值转换为到IN This will work in both 5.7 and 8.0:这适用于 5.7 和 8.0:

SELECT * FROM users WHERE JSON_UNQUOTE(JSON_EXTRACT(attributes, '$.connection')) in ('HTTP','API');

https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=ea3e311a775cf30e393b6ac07b7fde30 https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=ea3e311a775cf30e393b6ac07b7fde30

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

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