【问题标题】:How to SQL JOIN on concatenated columns in MS Access?如何对 MS Access 中的连接列进行 SQL JOIN?
【发布时间】:2016-05-15 21:23:44
【问题描述】:

我有 2 个表,其中 X 列和 Y 列连接起来表示一个唯一标识符。我想在tableB中找到tableA中不存在的所有行并将它们添加到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 不能用于大数据集?
  • 可以,但不符合我的目的。时间太长了

标签: sql ms-access join concatenation matching


【解决方案1】:

我认为速度慢不是由exists 引起的。您的查询可能很慢,因为您尝试使用连接来匹配多个列。请改用and,并确保在 (x,y) 上有一个复合索引:

这将选择 tableB 中所有在 tableA 中没有相同 (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
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2013-07-31
    相关资源
    最近更新 更多