8

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?

4
  • 1
    This isnt ideal to put json in the db, unless the tool is meant for it for example mongodb Commented Dec 12, 2014 at 17:29
  • 1
    It might not be "ideal," but serializing data in a SQL db is a pretty common practice. Commented Dec 12, 2014 at 17:37
  • yes, but if you need to access individual bits of data inside the serialized block,then your DB design is bad and shouldn't have had the data serialized in the first place.... or at least use a DB that understands that json natively. Commented Dec 12, 2014 at 17:42
  • Ok, but whats else out there to use. What if there are 8 users involved, what can array them into a db? Commented Dec 12, 2014 at 18:15

3 Answers 3

26

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

Sign up to request clarification or add additional context in comments.

Comments

4

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"))

Comments

1
SELECT startUser, 
forUser, 
percentage, 
time, 
taskTitle, 
taskDesc, 
color 
FROM admin_task WHERE JSON_SEARCH(forUser -> '$[*]', 'one', 'demo');

1 Comment

adding some comments to explain why/how it works will inrease the long-term value of the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.