【问题标题】:Concatenating all matches from a join table into a column将连接表中的所有匹配项连接到一列中
【发布时间】:2020-01-29 20:24:36
【问题描述】:

我有两个 MySQL 表(table_a 和 table_b)和一个连接表(table_c)。

表结构:

  • table_a:
__________________
| table_a:       |
|----------------|
| id             |
| result_column  |
------------------
  • table_b:
__________________
| table_b:       |
|----------------|
| id             |
| name           |
------------------
  • table_c:
__________________
| table_c:       |
|----------------|
| id             |
| table_a_id     |
| table_b_id     |
------------------

我的目标:

我想找到一个查询:

  1. 遍历每条table_a记录并获取table_a.id值
  2. 查找 table_c 中具有匹配 table_c.table_a_id 值的任何记录
  3. 对于 table_c 中的每个匹配记录,获取 table_c.table_b_id 值
  4. 在table_b中查找与table_b.id值匹配的记录
  5. 对于 table_b 中的匹配记录,获取 table_b.name 值
  6. 在table_a中,将每个匹配的名称值连接到对应的table_a.result_column中

示例: 查询前:

_______________________  _________________________________  ________________
| table_a:            |  | table_c:                      |  | table_b:     |
|---------------------|  |-------------------------------|  |--------------|
| id  | result_column |  | id  | table_a_id | table_b_id |  | id  | name   |
|-----|---------------|  |-----|------------|------------|  |-----|--------|
|  1  |               |  |  1  |      1     |      3     |  |  1  | Kevin  |
|  2  |               |  |  2  |      1     |      4     |  |  2  | Jesse  |
|  3  |               |  |  3  |      2     |      2     |  |  3  | Karen  |
-----------------------  |  4  |      3     |      1     |  |  4  | Tim    |
                         |  5  |      3     |      5     |  |  5  | Lauren |
                         ---------------------------------  ----------------

查询后:

_______________________  _________________________________  ________________
| table_a:            |  | table_c:                      |  | table_b:     |
|---------------------|  |-------------------------------|  |--------------|
| id  | result_column |  | id  | table_a_id | table_b_id |  | id  | name   |
|-----|---------------|  |-----|------------|------------|  |-----|--------|
|  1  | Karen, Tim    |  |  1  |      1     |      3     |  |  1  | Kevin  |
|  2  | Jesse         |  |  2  |      1     |      4     |  |  2  | Jesse  |
|  3  | Kevin, Lauren |  |  3  |      2     |      2     |  |  3  | Karen  |
-----------------------  |  4  |      3     |      1     |  |  4  | Tim    |
                         |  5  |      3     |      5     |  |  5  | Lauren |
                         ---------------------------------  ----------------

为了清楚起见,我知道这在关系数据表中是非常糟糕的做法。这与人们所能得到的标准化相去甚远。我永远不会设计这样的数据库。我的任务是为业务案例创建一个包含值列表的自定义列纯粹

【问题讨论】:

    标签: mysql sql join concatenation


    【解决方案1】:

    您似乎想要的查询是:

    select c.table_a_id, group_concat(b.name separator ', ') 
    from c join
         b
         on c.table_b_id = b.id
    group by c.table_a_id;
    

    如果你真的想更新a,你可以把它放到update 语句中:

    update a join
           (select c.table_a_id, group_concat(b.name separator ', ') as names
            from c join
                 b
                 on c.table_b_id = b.id
            group by c.table_a_id
           ) cb
           on cb.table_a_id = a.id
        set result_column = cb.names
    

    【讨论】:

      【解决方案2】:

      上一个答案很接近;但您还要求您只希望 A 中的表 C 中匹配的记录。

      第一个查询不满足这个要求;但是更新语句会,因为它只会更新表 A 中的记录,如果 id 与从表 C 中提取的 table_a_id 值匹配。

      鉴于你所说的你希望得到的最终结果,上面的更新语句会起作用。 如果您希望在您的逻辑中明确,只需添加从表 A 到表 C 的连接。

      select a.id, group_concat(b.name separator ', ') 
      from a 
      join c ON (a.id = c.table_a_id)
      join b ON (c.table_b_id = b.id)
      group by a.id;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2016-01-11
        • 2014-09-26
        • 2015-08-24
        • 1970-01-01
        • 2021-09-11
        • 2016-08-05
        相关资源
        最近更新 更多