Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql query WHERE column is in json array

Tags:

json

sql

php

mysql

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?

like image 377
MrDikke Avatar asked Dec 12 '14 17:12

MrDikke


People also ask

How do I query a JSON column in MySQL?

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.

How do I index a JSON column in MySQL?

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.

Can we index JSON column in MySQL?

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.

What is JSON extract () function in MySQL?

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]'); +------------------------------------------+


3 Answers

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

like image 171
ursuleacv Avatar answered Oct 11 '22 17:10

ursuleacv


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"))
like image 4
user1866795 Avatar answered Oct 11 '22 18:10

user1866795


SELECT startUser, 
forUser, 
percentage, 
time, 
taskTitle, 
taskDesc, 
color 
FROM admin_task WHERE JSON_SEARCH(forUser -> '$[*]', 'one', 'demo');
like image 1
Daniel Simiyu Avatar answered Oct 11 '22 16:10

Daniel Simiyu