MySQL JSON
- MySQL 5.7.8 ๋ถํฐ DBMS ์ฐจ์์์ 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
- MySQL ์์ JSON Data์ฌ์ฉํ๊ธฐ