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
)。它失败。有办法吗?
答案 0 :(得分:1)
在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