[英]MySQL : Using alias inside a join condition
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
I'm trying to use an alias ( profile_id
) inside my join condition. 我正在尝试在连接条件中使用别名(
profile_id
)。 It fails. 它失败。 Is there a way to do this?
有没有办法做到这一点?
Column aliases defined in th SELECT
clause cannot be used in the join conditions. SELECT
子句中定义的列别名不能在连接条件中使用。 This is because the FROM
clause is evaluated before the SELECT
clause. 这是因为
FROM
子句在SELECT
子句之前进行求值。
If I followed you correctly, you probably want: 如果我正确地跟随了您,则可能需要:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.