[英]MS Access: JOIN using Concatenated Field
I have a query with a JOIN on a couple Fields (ID_1 & ID_2). 我在两个字段(ID_1和ID_2)上有一个JOIN的查询。
My issue: The JOIN is not producing the desired result when ID_2 is Null 我的问题:ID_2为Null时,JOIN无法产生预期的结果
Can I JOIN on the concatenation of ID_1 & ID_2 at query time? 我可以在查询时加入ID_1和ID_2的串联吗? A few feeble attempts have not helped.
一些微弱的尝试没有帮助。
I'd prefer not to create a new Index in the 2 tables as one of them does not belong to me. 我宁愿不要在2个表中创建新索引,因为其中一个表不属于我。
Thanks for any advice. 感谢您的任何建议。
Simplified version of my query: 我的查询的简化版本:
SELECT Table1.ID_1, Table1.ID_2, Table1.Name, Table2.ID_1, Table2.ID_2, Table2.Owner
FROM Table1
LEFT JOIN Table2 ON (Table1.ID_2 = Table2.ID_2) AND (Table1.ID_1 = Table2.ID_1);
Using the query above, I see ID_1, ID_2 & Name from Table1, but nothing from Table2 when ID_2 is null 使用上面的查询,我看到表1中的ID_1,ID_2和名称,但是当ID_2为空时,表2中没有显示任何内容
Sample ("Alternative") Data: :) 样本(“替代”)数据::)
Table1: 表格1:
ID_1 ID_2 Name
===========================
A001 ACME Corp
A001 B001 ACME Medical
A001 B002 ACME Pharmacy
A002 General Electric Corp
A003 Philips
Table2: 表2:
ID_1 ID_2 Owner
===========================
A001 Bob
A001 B001 Lori
A001 B002 Becky
A002 Ravi
A003 Joe
When I LEFT JOIN, I expect to marry the info across the 2 tables. 当我离开JOIN时,我希望将信息与2张桌子一起使用。 The issue as describe presents the following result - ONLY joining in Owner when both ID_1 and ID_2 are both non-Null (Owner = Null is not the correct result):
所描述的问题会产生以下结果-仅在ID_1和ID_2都非null时才加入所有者(Owner = Null不是正确的结果):
Joined (Incorrect Result): 已加入(错误结果):
ID_1 ID_2 Name Owner
=========================================
A001 ACME Corp
A001 B001 ACME Medical Lori
A001 B002 ACME Pharmacy Becky
A002 General Electric
A003 Philips
Lets go over JOINS: 让我们来看看JOINS:
INNER JOIN - Only displays COMMON ROWS between both tables. 内部联接 -仅在两个表之间显示通用行 。 If the joined table has NULL in the COMMON FIELD YOUR JOINING ON , the row is not displayed.
如果联接的表在“ 您的联接的公共字段”中为NULL,则不显示该行。
LEFT JOIN - All rows from the left table included. 左联接 -包括左表的所有行。 Non-Matched entries from the right table will have NULL
右表中的未匹配条目将为NULL
RIGHT JOIN - All rows from the Right Table included. 右联接 -右表中的所有行都包括在内。 Non-Matched entries from the left table will have NULL
左表中的未匹配条目将为NULL
FULL JOIN - All rows from left and right table included. FULL JOIN-包括左表和右表的所有行。 Non-Matched entries from left and right table will have NULL.
左右表中的未匹配条目将为NULL。
Using a LEFT JOIN will give your desired result. 使用左联接将给您想要的结果。
Select A.ID_1
,B.ID_2
From tableA A
LEFT JOIN tableB B
ON A.ID_1 = B.ID_2
If you must live with the deficient table design, you might try a query like this: 如果必须使用不足的表设计,则可以尝试这样的查询:
SELECT Table1.ID_1, Table1.ID_2, Table2.OtherInfo
FROM Table1 INNER JOIN Table2 ON ((Table1.ID_2 = Table2.ID_2) AND (Table1.ID_1 = Table2.ID_1))
OR ((Table1.ID_1 = Table2.ID_1) AND Table1.ID_2 IS NULL AND Table2.ID_2 IS NULL)
Consider the following variations. 考虑以下变化。 Choose the easiest to maintain or most efficient type.
选择最容易维护或最有效的类型。
Union Query 联合查询
First SELECT
capture records with matching ids ( ID_1 = ID_2
) and second SELECT
captures records where only ID_1
match but ID_2 IS NULL
in both tables. 第一个
SELECT
捕获具有匹配ID( ID_1 = ID_2
)的记录,第二个SELECT
捕获两个表中只有ID_1
匹配但ID_2 IS NULL
记录。
SELECT t1.ID_1, t1.ID_2, t1.[Name], t2.Owner
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.ID_1 = t2.ID_1 AND t1.ID_2 = t2.ID_2
UNION ALL
SELECT t1.ID_1, t1.ID_2, t1.[Name], t2.Owner
FROM [Table1] t1
INNER JOIN [Table2] t2 ON t1.ID_1 = t2.ID_1
WHERE t1.ID_2 IS NULL AND t2.ID_2 IS NULL;
Alternatively, consider single SELECT
statements: 或者,考虑单个
SELECT
语句:
Explicit Join (with OR
in ON
clause) 显式联接 (在
ON
子句中使用OR
)
SELECT t1.ID_1, t1.ID_2, t1.[Name], t2.Owner
FROM [Table1] t1
INNER JOIN [Table2] t2 ON (t1.ID_1 = t2.ID_1 AND t1.ID_2 = t2.ID_2)
OR (t1.ID_1 = t2.ID_1 AND t1.ID_2 IS NULL AND t2.ID_2 IS NULL);
Implicit Join (query with no explicit JOIN
) 隐式联接 (没有显式
JOIN
查询)
SELECT t1.ID_1, t1.ID_2, t1.[Name], t2.Owner
FROM [Table1] t1, [Table2] t2
WHERE (t1.ID_1 = t2.ID_1 AND t1.ID_2 = t2.ID_2)
OR (t1.ID_1 = t2.ID_1 AND t1.ID_2 IS NULL AND t2.ID_2 IS NULL);
Each above should return (but not necessarily in same order) the following output: 以上每个都应返回(但不一定以相同的顺序)以下输出:
-- ID_1 ID_2 Name Owner
-- A001 ACME Corp Bob
-- A001 B001 ACME Medical Lori
-- A001 B002 ACME Pharmacy Becky
-- A002 General Electric Corp Ravi
-- A003 Philips Joe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.