使用部分字符串匹配前 n 个数字进行连接 PostgreSQL
我正在尝试根据 PostgreSQL 中的部分字符串匹配来连接两个表。例如,我有以下内容:
table1.code
010129
022933
029482
table2.new_code
010129847648
022933646495
029482732610
我想根据两个表中匹配的前 6 个字符进行连接。出于绝望,我尝试了类似以下的方法,但 Postgres 似乎不喜欢它。
SELECT table1.code, table2.new_code,
FROM table1
INNER JOIN table2
ON table1.code = LEFT(table2.code, 6)
有没有办法做我想做的事?
I am trying to join two tables based on a partial string match in PostgreSQL. For example, I have the following:
table1.code
010129
022933
029482
table2.new_code
010129847648
022933646495
029482732610
I want to join based on the first 6 characters matching across the two tables. I have tried something like the following out of desperation, but Postgres doesn't seem to like it.
SELECT table1.code, table2.new_code,
FROM table1
INNER JOIN table2
ON table1.code = LEFT(table2.code, 6)
Is there a way to do what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了一些语法问题之外,你所拥有的应该可以工作。 table2 中的列名为 new_code,选择列表中有一个额外的逗号。
What you have should work, except for a few grammar issues. The column in table2 is named new_code and there's an extra comma in the select list.