【发布时间】:2021-01-14 21:01:24
【问题描述】:
如果在第一次连接时满足条件,我需要连接不同的表,因此,如果第一次连接的列的值为 1,则与一个表进行连接,但如果值为 2,则使用另一个表,类似这样,但 MySQL 报错
SELECT col1, col2, col3,...,col8
FROM table1 AS tb1
INNER JOIN table2 AS t2 ON tb1.id = tb2.id
INNER JOIN table3 AS t3 ON tb2.id = tb3.id
CASE
WHEN tb2.col2 = 1 THEN INNER JOIN table4 ON id = col1
WHEN tb2.col2 = 2 THEN INNER JOIN table5 ON id = col3
WHEN tb2.col2 = 3 THEN INNER JOIN table6 ON id = col5
END
WHERE tb1.id = 13;
【问题讨论】: