尝试在邮政编码上加入 2 tables,其中两个 tables 都包含前 5 个匹配的邮政编码,但其他 table 包括 10 位邮政编码

trying to join 2 tables on zip code where both tables contain first 5 matching zips but other table includes 10 digit zips

这是我的资料。客户 table 中的邮政编码可能有 5 位或 10 位数字 邮政编码,其中县 table 中的邮政编码包含所有 5 位邮政编码。我认为 left(c.[Post Code], 5) 会截断连接中的 'on' 但不会。

 select     c.[No_], 
            c.[Name], 
            c.[Address],  
            c.[City], 
            left(c.[Post Code], 5), 
            kc.[County]
            from [Customer]c
            left join [Counties]kc on c.[Post Code] = kc.[Post Code]

我不想这么说,但您没有加入邮政编码。如果你是,你可以使用

where left(c.[Post Code], 5) = kc.whatever-the-post-code-value-is.

在这种情况下,您至少要 return 一次客户信息,并且可能与一个城市有多少个县一样多次。但是 postal 代码将永远是 customer.post 代码。