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) 명령어
[공지] 기술 게시판
3.31
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.10
4.11
4.12
4.13
4.14
4.15
4.16
4.18
4.19
4.20
4.21
4.22
4.23
4.24
4.25
4.26
4.27
4.28
4.29
4.30
5.1
5.2
5.3
5.4