Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

postgresql - Join column from a table with concat columns from another one

I try to match names from a table with a concatenation of columns in another table with Postgres.

What I have:

Table A:

id,name
1,John Smith
2,Laura Doe Van Renburg
3,Laura Thorpe
4,Carl Leonard Dong

Table B:

id,firstname,lastname
1,Aloys,Smith
2,Laura,Doe Van Renburg
3,Pedro,De Mung
4,Carl Leonard, Dong

The result I expect

Laura Doe Van Renburg
Carl Leonard Dong

What I tried

I think concatening the columns firstname and lastname from table B could help but I can't figure out what the correct syntax is.

select A.name from A 
  join (select concat(firstname,' ',lastname) from B) as firstandlast 
  on a.name = firstandlast;

But it's not the correct way. Any clue would be welcome!

question from:https://stackoverflow.com/questions/65931464/join-column-from-a-table-with-concat-columns-from-another-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You were close:

select a.name 
from table_a a
  join table_b b on concat(b.firstname, ' ', b.lastname) = a.name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...