[英]Mysql query WHERE column is in json array
I have setup an query where it selects multiple things. 我已经设置了一个查询,在查询中可以选择多个项目。
$stm = $db->query('SELECT startUser, forUser, percentage, time, taskTitle, taskDesc, color FROM admin_task WHERE forUser = "'.$queryanswer.'")');
But for user is like this in the DB: 但是对于用户来说,在数据库中是这样的:
["demo"," user"]
How can I check if forUser (the json array above) has demo? 如何检查forUser(上面的json数组)是否具有演示? can i even check this? 我什至可以检查一下吗?
I think you can achieve this only in Mysql 5.7. 我认为您只能在Mysql 5.7中实现。
In version 5.7 you can do something like: 在5.7版中,您可以执行以下操作:
SELECT JSON_EXTRACT(json_field, '$.name');
and it will extract only the name key from json object. 它将仅从json对象提取名称键 。
Search all items with the 'JavaScript' tag: 搜索所有带有“ JavaScript”标签的项目:
SELECT * FROM `table` WHERE JSON_CONTAINS(json_field, '["JavaScript"]');
Find all items with tags starting 'Java': 查找所有带有以“ Java”开头的标签的项目:
SELECT * FROM `table` WHERE JSON_SEARCH(json_field, 'one', 'Java%') IS NOT NULL;
use 'one' to find the first match or 'all' to find all matches 使用“一个”查找第一个匹配项,或使用“全部”查找所有匹配项
You can extract the Twitter nickname using a JSON path: 您可以使用JSON路径提取Twitter昵称:
SELECT name, json_field->"$.twitter" AS `twitter` FROM `user`;
You can also reference a JSON path in the WHERE clause to only return users with a Twitter account: 您还可以在WHERE子句中引用JSON路径,以仅返回具有Twitter帐户的用户:
SELECT name, profile->"$.twitter" AS `twitter` FROM `user` WHERE profile->"$.twitter" IS NOT NULL;
You can do more things like: 您可以做更多的事情,例如:
Creating JSON Values 创建JSON值
Normalization, Merging, and Autowrapping of JSON Values JSON值的规范化,合并和自动包装
Searching and Modifying JSON Values 搜索和修改JSON值
Comparison and Ordering of JSON Values JSON值的比较和排序
Aggregation of JSON Values JSON值的汇总
for more info please refer to: https://dev.mysql.com/doc/refman/5.7/en/json.html 有关更多信息,请参考: https : //dev.mysql.com/doc/refman/5.7/en/json.html
If it truly is a JSON field, in 5.7 you can do this: 如果确实是JSON字段,则在5.7中可以执行以下操作:
SELECT
startUser,
forUser,
percentage,
time,
taskTitle,
taskDesc,
color
FROM admin_task
WHERE JSON_CONTAINS(forUser->'$[*]', JSON_ARRAY("somestring"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.