mysql to json string with GROUP_CONCAT, JSON_OBJECT

mysql to json with GROUP_CONCAT, JSON_OBJECT

*. 쿼리 (json escape 처리됨)

SELECT 
CONCAT('[',GROUP_CONCAT(
JSON_OBJECT(
'io_id',io_id,
'io_price',io_price,
'io_stock_qty',io_stock_qty,
'io_noti_qty',io_noti_qty,
'io_use',io_use
)),']')
FROM `options`
WHERE p_no=10;


*. 쿼리2
SELECT 
CONCAT('[',GROUP_CONCAT(
CONCAT('{"io_id":"',io_id,
'","io_price":',io_price,
',"io_stock_qty":',io_stock_qty,
',"io_noti_qty":',io_noti_qty,
',"io_use":',io_use,'}'
)),']')
FROM `sm_product_option`
WHERE p_no=10;

#. 쿼리 3 (MySQL 5.7.22+)(from 레드팍스)
SELECT
JSON_ARRAYAGG(
JSON_OBJECT(
'io_id',io_id,
'io_price',io_price,
'io_stock_qty',io_stock_qty,
'io_noti_qty',io_noti_qty,
'io_use',io_use
))
FROM `options`
WHERE p_no=10;


*. 결과
[{"io_id": "XXL-4", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XXL-3", "io_price": 0, "io_stock_qty": 9997, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XXL-2", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XXL-1", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XL-4", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XL-3", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XL-2", "io_price": 0, "io_stock_qty": 9996, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "XL-1", "io_price": 0, "io_stock_qty": 9996, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "L-4", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "L-3", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "L-2", "io_price": 0, "io_stock_qty": 9989, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "L-1", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "M-4", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "M-3", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "M-2", "io_price": 0, "io_stock_qty": 9975, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "M-1", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "S-4", "io_price": 0, "io_stock_qty": 9995, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "S-3", "io_price": 0, "io_stock_qty": 9999, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "S-2", "io_price": 0, "io_stock_qty": 9989, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "S-1", "io_price": 100, "io_stock_qty": 9994, "io_noti_qty": 100, "io_use": "1"},
{"io_id": "수동입력", "io_price": 100, "io_stock_qty": 999, "io_noti_qty": 999, "io_use": "1"},
{"io_id": "AAAA", "io_price": 0, "io_stock_qty": 999, "io_noti_qty": 999, "io_use": "1"}]


댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
6.15
6.16
6.17
6.18
6.20
6.21
6.22
6.24
6.25
6.26
6.27
6.28
6.29
6.30
7.1
7.2
7.3
7.4
7.5