5

I have JSON type column named medium in my table

one of the field value like ["art", "nature"]

I want to get the rows with equivalent like query WHEREmedium LIKE %art%`

Refered this links but can't find a proper solution for this

https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

3
  • Is this array or string...? Commented Oct 12, 2017 at 5:44
  • field is an array Commented Oct 12, 2017 at 5:49
  • How the data stored in the medium table ? Commented Oct 12, 2017 at 5:59

2 Answers 2

8

You still can use LIKE with JSON fields. It is a valid solution if you want to include keywords in some generic search functionality.

What I've noticed so far but do not have an explanation for is that LIKE is case-sensitive when applied to JSON fields (at least it is happening in MySQL 5.7.24). To overcome this problem, you can convert JSON fields to strings and then use LIKE.

SELECT *
FROM my_table
WHERE CONVERT(medium using 'utf8') LIKE '%art%';
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thanks. Just slap a lowercase() on the operands and it's finding the rows I want.
-2

I would advise not using a json type in this fashion. For one, JSON column types aren't that great for querying, they're a pita, and means you may be locked to one SQL implementation over another. Instead, you could either setup a separate table that contains a 1:n relationship with each record maintaining each of the mediums OR have a single column that has a comma-delimited string value with all the mediums. That then makes LIKE queries much easier and simpler - and indexable.

Comments

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.