0

I have table workers and table worksheets.

This two tables are linked by column "worker_id"

In worksheets we have some data about workers as birthday, sex and other. This data is saved in one column as serialized array

something like:

{"photo":"image.png","sex":"male","birthday":"14.09.2018","phone":"8(777)777-7778"}

My task is to get workers over 20 years old and under 30 years old for example

How can I do it? Also in birthday key may be null values

I will be glad of any help

Baurzhan
  • 207
  • 4
  • 13

1 Answers1

2

You can use json_extract to get data from json in MySQL

set @json = '{"age": 25,"photo":"image.png","sex":"male","birthday":"14.09.2018","phone":"8(777)777-7778"}';
select json_extract(@json, '$.sex'); //returns "male"

I suppose you are looking to a query like this:

select *
  from workers w
 inner join worksheets ws on w.id = ws.worker_id
 where json_extract(ws.json_field, '$.age') > 20

Take a look at MySQL JSON functions MYSQL Json

Marcelo The Mage Coder
  • 1,944
  • 2
  • 11
  • 29