简体   繁体   English

如何使用SQL确定两个不同表中邮政编码的前几个字符?

[英]How to determine the first few characters of a postcode in two different tables with SQL?

So I've run into a bit of an issue. 所以我遇到了一个问题。 I can't seem to get my head around this problem and was wondering if I could get a bit of help. 我似乎无法解决这个问题,并且想知道是否可以得到一些帮助。

I've got two tables both containing postcode related information. 我有两个表都包含与邮政编码相关的信息。

Table A contains the full postcode, Table B contains the prefix for the area. 表A包含完整的邮政编码,表B包含该区域的前缀。

So for example table A contains the postcode NE628HJ and table b contains NE62. 因此,例如表A包含邮政编码NE628HJ,表b包含NE62。 How do I query it to select all the similar ones? 如何查询以选择所有类似的商品?

I've tried this: 我已经试过了:

SELECT * from tableB
left join tableA
       on tableA.postcode LIKE tableB.postcode

but this doesn't work. 但这不起作用。

Any suggestions? 有什么建议么?

You can use wildcards in the like : 您可以like使用通配符:

SELECT *
from tableB left join
     tableA
     on tableA.postcode LIKE tableB.postcode + '%';

Performance will not be particularly good, because this will not take advantage of indexes. 性能不会特别好,因为这不会利用索引。

You can use 您可以使用

SELECT * from tableB
left join tableA
       on left(tableA.postcode,2) = left(tableB.postcode,2)

to match on the 2 char prefix 匹配2个字符的前缀

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

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