sql >> Base de Datos >  >> RDS >> Mysql

Cómo poner JSON en una columna de datos si la subconsulta devuelve más de 1 fila en MySQL

Un poco complicado, pero podría crear objetos JSON para cada fila, concatenarlos usando GROUP_CONCAT y emitir el resultado (envuelto en [] para convertirlo en una matriz) a JSON;

SELECT u.uid, u.username, 
  CASE WHEN p.uid IS NULL 
       THEN NULL 
       ELSE CAST(CONCAT('[',
                   GROUP_CONCAT(JSON_OBJECT('pid', p.pid,
                                            'title', p.title, 
                                            'status', p.status)),     
                 ']') AS JSON) END portfolios
FROM user u
LEFT JOIN portfolio p
  ON u.uid=p.uid
WHERE p.status = 1
GROUP BY u.uid, u.username;

...que da...

+------+----------+-----------------------------------------------------------------------------------------------------------------------------------------+
|    1 | manoj    | [{"pid": 1, "title": "title 1", "status": 1}, {"pid": 2, "title": "title 2", "status": 1}, {"pid": 3, "title": "title 3", "status": 1}] |
|    2 | testing  | [{"pid": 4, "title": "title 1", "status": 1}]                                                                                           |
|    3 | user     | NULL                                                                                                                                    |
|    4 | company  | NULL                                                                                                                                    |
+------+----------+-----------------------------------------------------------------------------------------------------------------------------------------+

Si está utilizando un MySQL anterior sin compatibilidad con JSON, puede compilarlo como una cadena;

SELECT u.uid, u.username, 
  CASE WHEN p.uid IS NULL
  THEN NULL 
  ELSE CONCAT('[', 
    GROUP_CONCAT(CONCAT('{ "pid":',p.pid,',"title":"', REPLACE(p.title, '"', '\\"'),
           '","status":',p.status, ' }')), ']') END portfolios
FROM user u 
LEFT JOIN portfolio p 
  ON u.uid=p.uid AND p.status=1
GROUP BY u.uid, u.username;

...que te dará...

+------+----------+------------------------------------------------------------------------------------------------------------------------------+
| uid  | username | portfolios                                                                                                                   |
+------+----------+------------------------------------------------------------------------------------------------------------------------------+
|    1 | manoj    | [{ "pid":2,"title":"title 2","status":1 },{ "pid":3,"title":"title 3","status":1 },{ "pid":1,"title":"title 1","status":1 }] |
|    2 | testing  | [{ "pid":4,"title":"title 1","status":1 }]                                                                                   |
|    3 | user     | NULL                                                                                                                         |
|    4 | company  | NULL                                                                                                                         |
+------+----------+------------------------------------------------------------------------------------------------------------------------------+