DB/MySQL

JSON 타입에서 값 추출해서 JOIN하기

잔망루피 2023. 11. 21. 11:23
반응형

attraction_plan

 

attraction_info

 

-- attraction 필드 값 예시
[{"order": 1, "orderDate": 1, "attractionId": 1886054}, {"order": 2, "orderDate": 1, "attractionId": 1920563}, {"order": 1, "orderDate": 2, "attractionId": 136249}, {"order": 2, "orderDate": 2, "attractionId": 136310}]

attraction_plan 테이블에 있는 attraction 필드는 JSON 타입이고, JSON 객체를 담고 있는 배열이다.

attractionId와 관광지 테이블의 attraction_info 테이블의 content_id 필드의 값을 이용해서 조인을 했다.

 

 

select attractionId, at.title, at.first_image, at.addr1, at.content_id, orderNum, orderDate
from enjoytrip.attraction_plan ap,
json_table(ap.attraction, '$[*]' columns (
attractionId int path '$.attractionId',
orderNum int path '$.order',
orderDate int path '$.orderDate'
)) as jt
join attraction_info at on jt.attractionId = at.content_id
where attraction_plan_id = 1;
;

 

 

MyBatis로 쿼리를 작성해서 뽑아온 결과

 

반응형