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? can i even check this?
MySQL provides two operators ( -> and ->> ) to extract data from JSON columns. ->> will get the string value while -> will fetch value without quotes. As you can see ->> returns output as quoted strings, while -> returns values as they are. You can also use these operators in WHERE clause as shown below.
In MySQL, the only way to index a JSON path expression is to add a virtual column that mirrors the path expression in question and build an index on the virtual column. As you can see, the title column is mapped to the $. title path expression on the properties JSON column.
MySQL doesn't have a way to index JSON documents directly, but it has given us an alternative: generated columns. One thing that has been missing since MySQL added the JSON data type in version 5.7. 8, is the ability to index JSON values, at least directly.
We can use the JSON_EXTRACT function to extract data from a JSON field. The basic syntax is: JSON_EXTRACT(json_doc, path) For a JSON array, the path is specified with $[index] , where the index starts from 0: mysql> SELECT JSON_EXTRACT('[10, 20, 30, 40]', '$[0]'); +------------------------------------------+
I think you can achieve this only in Mysql 5.7.
In version 5.7 you can do something like:
SELECT JSON_EXTRACT(json_field, '$.name');
and it will extract only the name key from json object.
Search all items with the 'JavaScript' tag:
SELECT * FROM `table` WHERE JSON_CONTAINS(json_field, '["JavaScript"]');
Find all items with tags starting '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:
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:
SELECT name, profile->"$.twitter" AS `twitter` FROM `user` WHERE profile->"$.twitter" IS NOT NULL;
You can do more things like:
Creating JSON Values
Normalization, Merging, and Autowrapping of JSON Values
Searching and Modifying JSON Values
Comparison and Ordering of JSON Values
Aggregation of JSON Values
for more info please refer to: 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:
SELECT
startUser,
forUser,
percentage,
time,
taskTitle,
taskDesc,
color
FROM admin_task
WHERE JSON_CONTAINS(forUser->'$[*]', JSON_ARRAY("somestring"))
SELECT startUser,
forUser,
percentage,
time,
taskTitle,
taskDesc,
color
FROM admin_task WHERE JSON_SEARCH(forUser -> '$[*]', 'one', 'demo');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With