1

I have a table with adresses, which were separated by spaces into different columns. Since the name of the streets can be made up from 1-2-3 etc. parts, the street suffices are placed in different columns. I have a list of the possible street suffices and I want to check the columns and select the records which are matching to any of the street suffices.

For example if I have a table like:

C1     C2     C3     C4 
------------------------
Abbey  road
Made   up     street
Some   Other  Weird  lane
Time   square

And also I have the table containing the "list" of possible suffices:

C5
--------
avenue
road
street
way
lane
square
drive
boulevard
etc...

What I would like to have is something like this:

C5
--------
road
street
lane
square

Is there a possible way to do this? I was looking for similar questions, but couldn't find any.

1
  • Please tag your question with the database you are using. Also, what happens when multiple suffixes match? Or when no suffixes match? Commented Oct 16, 2019 at 15:25

2 Answers 2

1

You could join two tables using IN as condition:

SELECT *
FROM table_1 t1
JOIN table_2 t2
  ON t2.C5 IN (t1.c1, t1.c2, t1.c3, t1.c4)
1
  • 1
    Thank you very much, it works perfectly and really simple! I thought it would be something much more difficult.
    – 588chm
    Commented Oct 17, 2019 at 12:14
0

You can get the matches using a join:

select t.*, l.c5
from table1 t1 join
     list l
     on l.c5 in (t1.c1, t1.c2, t1.c3, t1.c4);

However, this may return multiple rows from the first table when there are multiple matches.

In your sample data, you might want to only match the last column. If so:

select t.*, l.c5
from table1 t1 join
     list l
     on l.c5 = coalesce(t1.c4, t1.c3, t1.c2, t1.c1);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.