使用case语句(或类似)的动态内连接

时间:2014-06-29 19:26:21

标签: mysql sql join

我正在尝试内连接 table1 < - to - > table2,table3,table4或table5 ,具体取决于 table1 中的字段。因此,如果 table1 中的字段 信息 的值 example_get_from_two ,我应该将 table1与table2内连接以获取给定的行,如果在另一行 table1 的值为 example_get_from_three ,我应该将 table1与table3 内连接,等等上。这是我试过的查询,但它返回零行:

SELECT n.notification_type, 
  (CASE WHEN n.notification_type = 'two' AND n.information = t2.somefield
            THEN t2.anotherfield
        WHEN n.notification_type = 'three' 
            THEN (CASE WHEN t3.field = '1' THEN t3.otherfield ELSE t3.yetanotherfield END)
        WHEN n.notification_type = 'four' AND t4.field = n.information 
            THEN t4.anotherfield
        WHEN n.notification_type = 'five' 
            THEN t5.field
   END) AS information FROM table0 zero, notifications n
        INNER JOIN table1 t1 ON(n.information=t1.somefield AND n.notification_type = 'something')
        INNER JOIN table2 t2 ON(n.information=t2.somefield AND n.notification_type = 'something')
        INNER JOIN table3 t3 ON((n.information=t3.somefield OR n.information=t3.someotherfield) AND n.notification_type = 'something') 
WHERE zero.field ='something' AND n.id = zero.id

不幸的是,这会检索零行,但不应该,因为所有联接都是在信息的实际值的情况下进行的,并且 n.notification_type =“something”。案件或类似情况可能吗?我想要获得的是

  

fromfield1 | fromfield2 | fromfield3(这个是“动态的”和   取决于联合表)

0 个答案:

没有答案