如何在Access中串联涉及链接的多行? [英] How to concatenate multiple rows in Access involving a Link?

查看:28
本文介绍了如何在Access中串联涉及链接的多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Access数据库存在以下问题:

我有3个表,分别是tblComponents,tblErrors和linkComponentsErrors.这是多对多的关系,这意味着一个组件可以有很多错误,而一个错误可以有很多组件.这些a与linkComponentsErrors中的primaryKey(tblComponents.componentID和tblErrors.errorID)链接在一起.这两个表都有一个名称字段

tblComponents/tblErrors/linkComponentsErrors

  | compID | compname || errID | errname || compID | errID |+ ------------ + + ------------ + + ------------ +|1 |HDD-300 ||1 |E101 ||1 |1 |+ ------------ + + ------------ + + ------------ +|2 |SSD-100 ||2 |E404 ||1 |2 |+ ------------ + + ------------ + + ------------ +|3 |CPU-i7 ||3 |E123 ||2 |2 |+ ------------ +|2 |3 |+ ------------ +|3 |3 | 

对于串联,我使用

I have the following problem concerning my Access Database:

I have 3 tables which are tblComponents, tblErrors, and linkComponentsErrors. This is a many to many relationships which means one component can have many errors and one error can have many components. These a are linked with their primaryKey (tblComponents.componentID and tblErrors.errorID) in the linkComponentsErrors. Both tables also have a field for their name

tblComponents / tblErrors / linkComponentsErrors

|compID|compname|        |errID|errname|        |compID|errID| 
+------------+           +------------+         +------------+
| 1 | HDD-300 |          | 1 | E101 |           | 1 | 1 |
+------------+           +------------+         +------------+
| 2 | SSD-100 |          | 2 | E404 |           | 1 | 2 |
+------------+           +------------+         +------------+
| 3 | CPU-i7 |           | 3 | E123 |           | 2 | 2 |
                                                +------------+
                                                | 2 | 3 |
                                                +------------+
                                                | 3 | 3 |

For the concatenation, I use the VBA-Code by Allen Browne

When I do this Query with the concatenation

SELECT tblComponents.compName, ConcatRelated("errorID", "linkComponentsErrors", "compID = " &[linkComponentsErrors].[compID]) FROM tblComponents;

I get this:

    |compName|Expr| 
    +------------+
    | HDD-300 | 1, 2 |
    +------------+
    | SSD-100 | 2, 3 |
    +------------+
    | CPU-i7 | 3 |
    +------------+

But what I want is this:

    |compName|Expr| 
    +------------+
    | HDD-300 | E101, E404 |
    +------------+
    | SSD-100 | E404, E123 |
    +------------+
    | CPU-i7 | E123 |
    +------------+

I am lost in the SQL Syntax in the Concat function with this join to the name of the error. I would be very happy if you know a solution to this problem.

解决方案

You can use my DJoin function for this:

SELECT 
    tblComponents.compname, 
    DJoin(
        "errname",
        "SELECT compID, errname 
            FROM linkComponentsErrors 
            INNER JOIN tblErrors ON linkComponentsErrors.errID = tblErrors.errID",
        "compID = " & [tblComponents].[compID] & "",
        ", ") AS errnames
FROM 
    tblComponents 
INNER JOIN 
    linkComponentsErrors ON tblComponents.compID = linkComponentsErrors.compID
GROUP BY 
    tblComponents.compname, 
    DJoin(
        "errname",
        "SELECT compID, errname 
            FROM linkComponentsErrors 
            INNER JOIN tblErrors ON linkComponentsErrors.errID = tblErrors.errID",
        "compID = " & [tblComponents].[compID] & "",
        ", "), 
    tblComponents.compID
ORDER BY 
    tblComponents.compID;

Output:

这篇关于如何在Access中串联涉及链接的多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆