SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulmn
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulmn
LEFT JOIN table4 AS t4
ON profile_id = t4.some_column
我正在尝试在连接条件中使用别名 (profile_id
)。它失败。有办法做到这一点吗?
最佳答案
SELECT
子句中定义的列别名不能在连接条件中使用。这是因为 FROM
子句在 SELECT
子句之前进行计算。
如果我正确地理解了你,你可能想要:
SELECT t1.*, IFNULL(t2.profile_id, t3.profile_id) AS `profile_id`
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.id = t2.some_coulm
LEFT JOIN table3 AS t3
ON t1.id = t3.some_coulm
LEFT JOIN table4 AS t4
ON IFNULL(t2.profile_id, t3.profile_id) = t4.some_column
关于MySQL : Using alias inside a join condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55483335/