Mysql 使用case语句(或类似语句)的动态内部联接
我正在尝试内部联接表1表2、表3、表4或表5,具体取决于表1中的字段。因此,如果表1中的字段信息具有值示例(u get_from_two我应该将表1与表2进行内部连接以获得给定行,如果在另一行中表1具有值示例(u get_from_two我应该将表1与表3进行内部连接以此类推。这是我尝试过的查询,但它返回了零行:Mysql 使用case语句(或类似语句)的动态内部联接,mysql,sql,join,Mysql,Sql,Join,我正在尝试内部联接表1表2、表3、表4或表5,具体取决于表1中的字段。因此,如果表1中的字段信息具有值示例(u get_from_two我应该将表1与表2进行内部连接以获得给定行,如果在另一行中表1具有值示例(u get_from_two我应该将表1与表3进行内部连接以此类推。这是我尝试过的查询,但它返回了零行: SELECT n.notification_type, (CASE WHEN n.notification_type = 'two' AND n.information = t2
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\u type=“something”。这是否适用于案例或类似案例?我想得到的是
fromfield1 | fromfield2 | fromfield3(这一个是“动态的”并且
取决于联接的表)
使用
左连接
instead@juergend太多了