1

I have two tables in a database.

Both contain a related id but in a different format:

table1.field = 123456-12-34
table2.id = 123456

What Im trying to do is a join on table1.id = table2.field

so the query looks like:

select name from 
    table1 left join table2 on table1.field like table2-%
    where table2.flag='1' and DATEDIFF( now(), table1.timestamp ) > 2

I know this isn't correct, but how do i join two tables on fields where they are related but not the same?

1

1 Answer 1

3

You want to use concat() to create the pattern string:

select name
from table1 left join
     table2
     on table1.field like concat(table2.id, '-%')
where table2.flag='1' and DATEDIFF( now(), table1.timestamp ) > 2
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.