使用部分字符串匹配前 n 个数字进行连接 PostgreSQL

发布于 2025-01-17 06:10:19 字数 410 浏览 3 评论 0原文

我正在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-01-24 06:10:19

除了一些语法问题之外,你所拥有的应该可以工作。 table2 中的列名为 new_code,选择列表中有一个额外的逗号。

SELECT table1.code, table2.new_code
FROM table1 
INNER JOIN table2 
ON table1.code = LEFT(table2.new_code, 6);

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.

SELECT table1.code, table2.new_code
FROM table1 
INNER JOIN table2 
ON table1.code = LEFT(table2.new_code, 6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文