Mysql连接问题
我遇到这个问题:
我有两张表:一张包含用户数据,一张包含分配给用户的邮政编码(一个用户可以有多个邮政编码)。
包含邮政编码的表有两列 - 开始和结束。首先,我需要选择 START 和 END 并将它们分开,例如。和 -。我知道我可以使用 CONCAT_WS 来执行此操作,但我还需要将此连接字符串(有多行)并将它们连接成一行。
我已经有这个查询:
SELECT pc.user_id, CONCAT_WS('-', pc.start, pc.end) FROM postal_codes pc, users u WHERE u.id=pc.user_id
但是你期望它给我这样的结果:(抱歉,我不知道如何在这里插入表)
row | user_id | postal range
============================
1 | 1 | AAAA-BBBB
----------------------------
2 | 1 | CCCC-DDDD
----------------------------
3 | 1 | MMMM-NNNN
----------------------------
4 | 2 | CCCC-DDDD
----------------------------
5 | 2 | EEEE-FFFF
----------------------------
并且我需要它像这样:
row | user_ID | postal_range
----------------------------
1 | 1 | AAAA-BBBB, CCCC-DDDD, MMMM-NNNN
----------------------------
2 | 2 | CCCC-DDDD, EEEE-FFFF
----------------------------
我知道如果我想连接,我可以使用 GROUP_CONCAT多行 - 但如果我将上一个查询的结果包装到 GROUP_CONCAT 中,如下所示:
SELECT pc.user_id, (GROUP_CONCAT((SELECT CONCAT_WS('-', pc.start, pc.end) FROM bs_postal_codes pc, bs_users u WHERE u.id=pc.user_id) SEPARATOR ', ')) FROM bs_postal_codes pc
它给了我错误:子查询返回超过 1 行
如果有人帮我解决这个问题,这将真正帮助我,谢谢。
I have this problem:
I have two tables: one with user data and one with postal codes assigned to user (one user can have multiple postal codes).
Table with postal codes has two columns - start and end. First I need to select START and END and separate them eg. with -. I know I can use CONCAT_WS to do this, but I also need to take this concatenated strings (there are multiple rows) and concatenate them into one row.
I already have this query:
SELECT pc.user_id, CONCAT_WS('-', pc.start, pc.end) FROM postal_codes pc, users u WHERE u.id=pc.user_id
But s you expect it gives me results like this: (sorry I don't know how to insert tables here)
row | user_id | postal range
============================
1 | 1 | AAAA-BBBB
----------------------------
2 | 1 | CCCC-DDDD
----------------------------
3 | 1 | MMMM-NNNN
----------------------------
4 | 2 | CCCC-DDDD
----------------------------
5 | 2 | EEEE-FFFF
----------------------------
and I need it to be like this:
row | user_ID | postal_range
----------------------------
1 | 1 | AAAA-BBBB, CCCC-DDDD, MMMM-NNNN
----------------------------
2 | 2 | CCCC-DDDD, EEEE-FFFF
----------------------------
I know I can use GROUP_CONCAT if i want to concatenate multiple rows - but if I wrap result of previous query into GROUP_CONCAT like this:
SELECT pc.user_id, (GROUP_CONCAT((SELECT CONCAT_WS('-', pc.start, pc.end) FROM bs_postal_codes pc, bs_users u WHERE u.id=pc.user_id) SEPARATOR ', ')) FROM bs_postal_codes pc
It gives me error: Subquery returns more than 1 row
It would really help me if someone give me a hand with this, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GROUP_CONCAT 与所有聚合函数一样,在它们所属的查询(外部查询)内工作。您不能将 SELECT 语句添加为返回多行的参数。
试试这个:
添加(见评论):
1.
2.
GROUP_CONCAT, as all aggregate functions, work within the query they are part of (the outer query). You cannot add a SELECT statement as a parameter that returns more than one row.
try this:
Addition (see comments):
1.
2.