MySQL JSON

DDL

CREATE TABLE employees (
  id integer  AUTO_INCREMENT primary key,
  name VARCHAR(200),
  profile JSON
);

DML insert

insert into employees(name, profile) values('ํ™๊ธธ๋™', '{ "age" : 30, "gender" : "man", "๋ถ€์„œ": "๊ฐœ๋ฐœ" }');
insert into employees(name, profile) values('ํ—ˆ๊ด‘๋‚จ', json_object(
    'age', 51,
    'gender', 'man',
    '๋ถ€์„œ', '์—ฐ๊ตฌ'
));
insert into employees(name, profile) values('์ „์€์ˆ˜', json_object(
    'age', 29,
    'gender', 'woman',
    '๋ถ€์„œ', '๊ฐœ๋ฐœ',
    '์ž๊ฒฉ์ฆ', json_array('CISA', 'PMP', 'CISSP')
    ));

๊ฐ€์ ธ์˜ค๊ธฐ

-- not working
select id,name,json_extract(profile, '$."๋ถ€์„œ"')
from employees where json_extract(profile, '$."๋ถ€์„œ"') = '๊ฐœ๋ฐœ';
-- MySQL ์˜ "unquotes the extracted result" ์—ฐ์‚ฐ์ž์ธ ->> ๋ฅผ ์‚ฌ์šฉํ•ด์„œ Quote ๋ฅผ ์ œ๊ฑฐ
select id,name, profile ->> '$."๋ถ€์„œ"', json_extract(profile, '$."๋ถ€์„œ"')
from employees where profile ->> '$."๋ถ€์„œ"' like '๊ฐœ%';
select id, name, json_extract(profile, '$.age')
from employees where json_extract(profile, '$.age') >= 35;

ref

What Else?
inflearn react api server -50% ํ• ์ธ์ฟ ํฐ: 20652-ab1f1cd4c373 buy me a coffee