[英]How to SQL JOIN on concatenated columns in MS Access?
我有2个表,其中X和Y列被连接起来以表示唯一标识符。 我想找到tableB中不存在的所有行,并将它们添加到tableC中。
-------tableA-------- // tableA is a master refernce table with all names so far
|__X__|__Y__|_name__|
| 3 | 7 | Mary |
| 3 | 2 | Jaime |
-------tableB-------- // tableB is an input file with all daily names (some repeats already exist in tableA)
|__X__|__Y__|_name__|
| 2 | 5 | Smith |
| 3 | 7 | Mary |
-------tableC-------- // tableC is a temporary holding table for new names
|__X__|__Y__|_name__|
| | | |
DESIRED RESULT:
-------tableC-------- // tableB - tableA = tableC
|__X__|__Y__|_name__|
| 2 | 5 | Smith |
我想根据串联的X + Y值来匹配行。 到目前为止,我的SQL查询如下所示:
INSERT INTO tableC
SELECT * FROM tableA
LEFT JOIN tableB
ON tableA.X & table.B = tableB.X & tableB.Y
WHERE tableB.X & tableB.Y IS null
但是,这不能给我预期的结果。 我不能使用EXISTS,因为我的实际数据集很大。 有人可以给我建议吗?
我不认为缓慢是由于exists
造成的。 您的查询速度可能很慢,因为您尝试使用串联来匹配多个列。 使用and
,并确保您在(x,y)上具有复合索引:
这将选择tableB中所有在表A中没有相同(x,y)值的唯一行。 注意,任何具有相同x,y但名称不同的行都将显示在结果中(即2,5,Joe也将出现)。 如果您不想这样做,则必须将x,y分组,并确定x,y重复但名称不同的情况下所需的名称。
select distinct x,y,name
from tableB b
where not exists (
select 1 from tableA a
where a.x = b.x
and a.y = b.y
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.