์๋ก ๋ค๋ฅธ ํ
์ด๋ธ์ ์ ๋ณด๋ฅผ ํ๋๋ก ํฉ์ณ์ ๋ณด์ฌ์ค๋ค.
ex) a class ๋ผ๋ ํ
์ด๋ธ๊ณผ b class๋ผ๋ ํ
์ด๋ธ์ด ๋๋์ด์ ธ ์๋๋ฐ ์ด ๋ ํ
์ด๋ธ์ ๋ด๊ธด
๋ชจ๋ ํ์๋ค์ ์ ๋ณด๋ฅผ ํ๋๋ก ๋ณด์ฌ์ฃผ๊ณ ์ถ์ ๋ ํด๋น ๊ธฐ๋ฅ์ ์ธ ์ ์๋ค.
union์ ์ค๋ณต๋๋ ๊ฐ์ ์ ๊ฑฐํ๊ณ ๋ณด์ฌ์ฃผ๋๋ฐ์ ๋ฐํด union all์ ์ค๋ณต ๊ฐ๊น์ง ํฌํจํ์ฌ ๋ณด์ฌ์ค๋ค.
SELECT id, name, age
FROM a_class_students
where id = '123'
union
select id, name, age
from b_class_students
where id ='123'
order by updated_date desc
select ๋ค์ ์ค๋ ์ปฌ๋ผ๋ช ๊ณผ ์ปฌ๋ผ ๊ฐ์๋ ๋์ผํด์ผ ํ๋ค. ๋ง์ฝ ์ปฌ๋ผ๋ช ์ด ๋ค๋ฅด๋ฉด user_id as id ๋ฑ์ ๋ณ์นญ์ ์ฃผ์ด ๋ง์ถฐ์ค๋ค. where์ ๋ฐ order by ์ดํ์ ์กฐ๊ฑด์ ์ ํ์ด๋ค.
left join์ ์ธ ๋์๋ group_by๋ฅผ ๋ถ์ฌ์ ์ค๋ณต๋๋ ๊ฐ์ ์ ๊ฑฐํ๋ค.
group by (์กฐ๊ฑด)์ ๋ฐ๋ฅธ ๊ฒฐ๊ณผ ๊ฐ์ ํฉgroup_concat์ ์ฐ๋ฉด ์ ์ฉํ๋ค.
group_concat(๊ฒฐ๊ณผ column) from table_name group by (์กฐ๊ฑด column)
table : old_school
class | student_name |
---|---|
A | ํ๊ธธ๋ |
A | ์๊บฝ์ |
A | ํฉ์ง์ด |
select class, group_concat(student_name) from old_school group by (class)
class | student_name |
---|---|
A | ํ๊ธธ๋, ์๊บฝ์ , ํฉ์ง์ด |
group_concat(name separator '+') => + ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ. (๊ตฌ๋ถ์)
class | student_name |
---|---|
A | ํ๊ธธ๋ + ์๊บฝ์ + ํฉ์ง์ด |
mysql์์ json ๊ฐ์ ์ฝ๊ณ , ๋ฃ๋ ๋ฐฉ๋ฒ
select JSON_EXTRACT(ํ ์ด๋ธ๋ช .์ปฌ๋ผ๋ช ,'$.key[*].value.key.value....') from ํ ์ด๋ธ๋ช ;
์๋ฅผ ๋ค๋ฉด ์๋์์ a_price์ count ๊ฐ์ธ 1์ ์ป๊ณ ์ถ๋ค๊ณ ํ์ ๋
select JSON_EXTRACT(brand_table.information,'$.price[*].a_price.count') as a_price_count from brand_table;
table ๋ช : brand_table
brand | information |
---|---|
brand A | {"price": [{"number": {"count": 0}, "a_price": {"count": 1}, "b_price": {"count": 2},"d_price"], "brand_logo": "lotte"}]} |
ํ ์ด๋ธ ์์ฑ
create table class (name varchar(20), student_profile json)
gp.price,'$.price_info[*].msrp_price.value'
'key','value' ํ์์ผ๋ก insert
insert into class(name, student_profile) values('ํ๊ธธ๋', json_object(
'age', 10,
'gender', 'man',
'grade', 'B+'
));
select * from class;
class | student_name |
---|---|
ํ๊ธธ๋ | {"age": 10, "grade": "B+", "gender": "man"} |