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}}
    -
제목 작성자 날짜
공대여자
공대여자
mins01
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자
공대여자