1

I have a 'jobs' table like this:

----------------------------------------------
|job_id |name                    |skills     |
----------------------------------------------
|1      |Job 1                   |[1]        |
|2      |Job 2                   |[2,3]      |
|3      |Job 3                   |[4,5,6]    |
----------------------------------------------

The 'skills' column contains a JSON array.

I need to select jobs where one or more skills are met - something like this (but obviously won't work):

SELECT * FROM jobs WHERE skills IN (1,4)

Should return:

----------------------------------------------
|job_id |name                    |skills     |
----------------------------------------------
|1      |Job 1                   |[1]        |
|3      |Job 3                   |[4,5,6]    |
----------------------------------------------
Ravi
  • 30,829
  • 42
  • 119
  • 173
Simon Lobo
  • 39
  • 1
  • 4

3 Answers3

2

You could use REGEXP

select * from jobs where skills REGEXP '[[:<:]]1[[:>:]]|[[:<:]]4[[:>:]]'

Append, [[:<:]] before and [[:>:]] after, to match exact value and | to match for multiple values.

>>>Demo<<<

Ravi
  • 30,829
  • 42
  • 119
  • 173
0

Either of these queries will return your expected output.

Option 1

If skills is a string (varchar, text) you could use the LIKE string comparison function.

SELECT * FROM jobs WHERE skills LIKE "%1%" OR skills LIKE "%4%";

Option 2

Or, if you prefer regular expressions, you could use the REGEXP function.

SELECT * FROM jobs WHERE skills REGEXP "1|4";

Joshua Powell
  • 894
  • 1
  • 9
  • 13
0

Storing JSON on the database is suitable for cases when you don't need to report or query on said fields, since it is less efficient, and doesn't take full advantage of years of query performance optimisations made by the database vendors. I advice using 2 tables, one for jobs and another for skills, and having a field called job_id on the skills table so you can simply do a query that looks like this:

select jobs.* from jobs where job.id in (select job_id in skill where skill_id in (1,4))

the skill table could later be expanded to have more fields or to be connected to other skills perhaps.

Hope that helps!

Felipe Valdes
  • 1,998
  • 15
  • 26