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

前端 未结 3 1372
野性不改
野性不改 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条回答
  •  -上瘾入骨i
    2021-01-01 18:13

    select t1.id as table1_id, 
        t2.id as table2_id, 
        t2.table3_id_1, 
        t2.table3_id_2,
        t3_1.value as X, 
        t3_2.value as Y
    from Table1 t1
    inner join Table2 t2 on t1.id = t2.table1_id
    inner join Table3 t3_1 on t2.table3_id_1 = t3_1.id
    inner join Table3 t3_2 on t2.table3_id_2 = t3_2.id
    where t3_1.value = 'some_value'
        or t3_2.value = 'some_other_value'
    

提交回复
热议问题