Join table twice - on two different columns of the same table

前端 未结 3 1407
野性不改
野性不改 2021-01-01 17:43

I have a very confusing database with a table that holds two values I need in a separate table. Here is my issue:

Table1
- id

Table2
- id
- table1_id
- tabl         


        
3条回答
  •  长发绾君心
    2021-01-01 18:08

    If you join a table several times, use aliases to distinguish them:

    SELECT table1.id,table2.id,table2.table3_id_1,table2.table3_id_2,t3_1.id,t3_2.id
    FROM table1
    JOIN table2 ON table1.id=table2.table1_id
    JOIN table3 t3_1 ON table2.table3_id_1=t3_1.id
    JOIN table3 t3_2 ON table2.table3_id_2=t3_2.id
    WHERE ... t3_1.id=... AND ... t3_2.id=...
    

提交回复
热议问题