Mysql连接问题

发布于 2024-12-05 05:01:50 字数 1318 浏览 2 评论 0原文

我遇到这个问题:

我有两张表:一张包含用户数据,一张包含分配给用户的邮政编码(一个用户可以有多个邮政编码)。

包含邮政编码的表有两列 - 开始和结束。首先,我需要选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-12 05:01:50

GROUP_CONCAT 与所有聚合函数一样,在它们所属的查询(外部查询)内工作。您不能将 SELECT 语句添加为返回多行的参数。

试试这个:

SELECT 
  pc.user_id, 
  GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ') as codes
FROM 
  bs_postal_codes pc
GROUP BY
  pc.user_id

添加(见评论):

1.

SELECT 
  u.id, 
  /* Optional other user fields. Add them to Group By too, */
  GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ') as codes
FROM 
  users u
  LEFT JOIN bs_postal_codes pc ON pc.user_id = u.id
GROUP BY
  u.id

2.

SELECT 
  u.id, 
  /* Optional other user fields. Add them to Group By too, */
  (SELECT
    GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ')
  FROM 
    bs_postal_codes pc
  WHERE
    pc.user_id = u.id) as codes
FROM 
  users u

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:

SELECT 
  pc.user_id, 
  GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ') as codes
FROM 
  bs_postal_codes pc
GROUP BY
  pc.user_id

Addition (see comments):

1.

SELECT 
  u.id, 
  /* Optional other user fields. Add them to Group By too, */
  GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ') as codes
FROM 
  users u
  LEFT JOIN bs_postal_codes pc ON pc.user_id = u.id
GROUP BY
  u.id

2.

SELECT 
  u.id, 
  /* Optional other user fields. Add them to Group By too, */
  (SELECT
    GROUP_CONCAT(CONCAT_WS('-', pc.start, pc.end) SEPARATOR ', ')
  FROM 
    bs_postal_codes pc
  WHERE
    pc.user_id = u.id) as codes
FROM 
  users u
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文