如果子查询在MySQL中返回超过1行,如何将JSON放入列数据中 [英] How to put JSON into a column data if sub-query returns more than 1 row in MySQL
问题描述
我要使用与选择用户相同的查询来选择用户投资组合.
I want to select portfolio of user with the same query that i used for select users.
这是我想要的例子.
用户表
----------------------------------------------------------------------
UID NAME USERNAME EMAIL PASSWORD STATUS
----------------------------------------------------------------------
1 Manoj manoj a@a.com ******** 1
2 Test U testing b@a.com ******** 1
3 Company user c@a.com ******** 1
4 Agency company d@a.com ******** 1
我的数据库中的用户表
User table in my database
投资组合表
-----------------------------------
PID UID TITLE STATUS
-----------------------------------
1 1 title 1 1
2 1 title 2 1
3 1 title 3 1
4 2 title 1 1
我的数据库中的投资组合表 我想要的结果
Portfolio Table in my database Result I want
----------------------------------
UID USERNAME PORTFOLIO
----------------------------------
1 manoj JSON OBJECT OF PID (1,2,3)
2 testing JOSN OBJECT OF PID (4)
3 user NULL
4 company NULL
当前我正在尝试使用
SELECT u.uid,u.username,(SELECT * FROM portfolio p WHERE u.UID=p.UID) as portfolio FROM users u
这个问题有解决方案吗?
Is there any solution for this problem?
推荐答案
有点费解,但是您可以为每行创建JSON对象,使用GROUP_CONCAT
将它们连接起来并转换结果(包装在[]
中以使其成为结果)数组)转换为JSON;
A bit convoluted, but you could create JSON objects for each row, concatenate them using GROUP_CONCAT
and cast the result (wrapped in []
to make it an array) to 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;
...给出...
+------+----------+-----------------------------------------------------------------------------------------------------------------------------------------+
| 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 |
+------+----------+-----------------------------------------------------------------------------------------------------------------------------------------+
如果您使用的是不支持JSON的旧版MySQL,则可以将其构建为字符串;
If you're using an older MySQL without JSON support, you could build it as a string;
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;
...这会给你...
...which will give you...
+------+----------+------------------------------------------------------------------------------------------------------------------------------+
| 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 |
+------+----------+------------------------------------------------------------------------------------------------------------------------------+
这篇关于如果子查询在MySQL中返回超过1行,如何将JSON放入列数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!