0

I have two tables tableA and tableB.

I want to write a query where it displays name and id if the string value of tableA's id is a substring of tableB's name. I tried the like operator but can't seem to get it to work.

   tableA
id   |
bob
cat
sky
   tableB
name   |
bobby
catlyn
bret

answer table
id     |name   | 
bob    bobby
cat    catlyn 
5
  • 1
    post your tried query too. and dbms. Commented Nov 24, 2020 at 9:40
  • What engine is it? Commented Nov 24, 2020 at 9:41
  • also this is not a proper way to handle data. you should have keys to bind the table. Commented Nov 24, 2020 at 9:45
  • What is exactly "doesn't work"? Your results are not the same as you've expected? So provide your query and it's output. If you have an error, then add the error message as plain text. Commented Nov 24, 2020 at 9:51
  • This was just an example data. I tried something like this "where tableB like %tableA.id%". But this doesn't work as it is not a string value and using + to concatenate doesn't work. Commented Nov 24, 2020 at 9:55

1 Answer 1

1

You can join on a like condition:

select a.id, b.name
from tablea a
inner join tableb b on b.name like '%' || a.id || '%'

You did not tell which database you are running, so this uses standard string concatenation operator ||; some databases use something else (eg: MySQL and SQLServer have function concat()).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.