Search code examples
mysqljoinsql-like

mysql join query with like?


Here is a representation of my db structure with a couple example rows. What I am attempting to accomplish is to take the address from table 1 and search in table 2 for a like match. Then I just need it to return the sub column result

    Table 1
    id | address        | info
    ----------------------------------------
    1  | 123 test dr    | blah blah blah
    2  | 456 testing ln | blah blah blah blah

    Table 2
    id | wo        | addr           | sec    | sub
    ------------------------------------------------
    1  | 12345678  | 123 TEST DR    | Sec. 1 | Sub1
    2  | 87654321  | 456 TESTING LN | Sec. 2 | Sub2

I have tried some join queries however I just can't seem to get it to work


Solution

  • Try to use something like:

    select t2.sub 
    from   Table1 t1
           left join Table2 t2 on t2.add like CONCAT('%', t1.address,'%')