Joining 2 sql tables based on substring

前端 未结 1 482
一向
一向 2021-01-24 13:39

I am trying to join tables where the only 2 keys which match are table1.Account and table2.key

But the problem is the setup. The table1.A

相关标签:
1条回答
  • 2021-01-24 13:53

    SQL has a substring function:

    https://msdn.microsoft.com/en-us/library/ms187748.aspx

    SELECT DISTINCT column1, column2
    from table1 
    INNER JOIN table2 
        ON table1.Account = substring(table2.key, 3, 4)
        OR table1.Account = substring(table2.key, 4, 4)
    order by column1
    

    I don't know if you need the 'or' in your on clause, but based on your question it seems like there may be two ways for those fields to match up. Regardless, you can modify the on clause, as needed, but this sample should help you with the syntax, which appears to be your obstacle.

    0 讨论(0)
提交回复
热议问题