Join two SQL tables that do not have a foreign key

0

What kind of friends, I'm helping in a project in Laravel with a SQL database quite badly done, anyway ...

I have Table1 with a field called Code and Table2 with the field called Code, the problem is that I must join the tables using the Code field, but the content of the Code field of each table is different, example:

Table1.Codigo = 14EPR0199Y - Table2. Code = 02 030 005 14EPR0199Y

How can I join the two tables by Codigo?

    
asked by Ricardo Garcia 06.09.2017 в 21:38
source

1 answer

1

Try this way:

Select * from Tabla1 t1
    join Tabla2 t2
        on Left(t1.Codigo,10) = Right(t2.Codigo,10)

Doing it, you can do it, you will not be able to optimize the query so you have indexes on the Code fields since the LEFT and RIGHT functions will not allow it, but if they are small tables, you should not have problems.

    
answered by 06.09.2017 / 21:58
source