简体   繁体   English

连接两个表,列出第一个表的所有结果

[英]Joining two tables listing all the results from the first table

I have two tables related to each other. 我有两个相互关联的表。 The first table has one or more number of the same attribute on the latter table. 第一个表在后一个表上具有一个或多个相同属性。 (there is a one-to-many relationship) thus the rows on the latter table includes basically the following columns: (PK, FK, attribute ). (存在一对多关系),因此后者表上的行基本上包括以下列:(PK,FK, attribute )。 Some rows of the first table does not have an attribute and are not recorded in the latter table. 第一个表的某些行没有属性,也没有记录在后一个表中。

I would like to list all the rows in the first table with an extra column for the attribute from the latter table. 我想列出第一个表中的所有行,并为后一个表中的属性添加一个额外的列。 when I use inner join or where clause I cannot list the results that have no attribute. 当我使用内部联接或where子句时,我无法列出没有属性的结果。 However what I want to do is to list all the rows from the first table, and if there is no attribute for it I want to make the extra column null, and if it has more than one attribute I want to concatenate them in that single column . 但是我要做的是列出第一个表中的所有行, 如果没有它的属性,我想使多余的列为null,如果它有多个属性,我想将它们合并到单个表中专栏

I am aware that I am not sharing any code here and I am asking the question in an abstract way because first I need to know which tool to use and in what way. 我知道我不在这里共享任何代码,而是以抽象的方式提出问题,因为首先我需要知道使用哪种工具以及以哪种方式使用。 I tried joins and sub-queries with select statement as well as methods such as ISNULL but I was not sure what I was doing and it did not work for me. 我尝试使用select语句以及诸如ISNULL之类的方法进行联接和子查询,但是我不确定自己在做什么,并且它对我不起作用。

PS: I am new to the site as you can observe. PS:您可以观察到,我是这个网站的新手。 Thus if you think this is not a proper way of asking questions and if you suggest removing this question off the forum I will no question asked. 因此,如果您认为这不是提出问题的适当方式,并且如果您建议从论坛中删除此问题,那么我将不会提出任何问题。

SELECT TableA.*, TableB.Attribute
FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.FK

Edit: I just read the original question more fully and realised that this is not quite what you're after. 编辑:我只是更完整地阅读了原始问题,并且意识到这与您追求的不完全相同。 This will give you the NULL value you wanted if there is no related data in TableB, however you will need to write a function if you want multiple related values in TableB concatenated into the one row for a given TableA record. 如果TableB中没有相关数据,这将为您提供所需的NULL值,但是,如果要将TableB中多个相关值串联到给定TableA记录的一行中,则需要编写函数。 Create a new module and in here add the following function. 创建一个新模块,然后在此处添加以下功能。 I am assuming here that your PK and FK columns are Long Integer and the Attribute values are String. 我在这里假设您的PK和FK列是Long Integer,而Attribute值是String。 I am also assuming that you're working in MS-Access. 我还假设您正在使用MS-Access。

Public Function ListAttributes(lngPK As Long) As String

    Dim rsAttributes As DAO.Recordset
    Dim strResult as String

    Set rsAttributes = CurrentDB.OpenRecordset("SELECT Attribute FROM TableB WHERE FK = " & lngPK)
    Do While Not rsAttributes.EOF
        strResult = strResult & ", " & rsAttributes!Attribute
        rsAttributes.MoveNext
    Loop
    If strResult <> "" Then
        strResult = Mid(strResult, 3)
    End If
    ListAttributes = strResult
    Set rsAttributes = Nothing

End Function

Your query then becomes 您的查询将变为

SELECT *, ListAttributes(PK) FROM TableA

Note that you will now get an empty string instead of NULL for rows in TableA that have no corresponding rows in TableB, but you can always use an IIf function call to fix this if necessary. 请注意,对于TableA中没有对应行的TableA行,您现在将获得一个空字符串而不是NULL,但是如有必要,您始终可以使用IIf函数调用来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM