Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:在连接条件中使用别名_Mysql_Sql - Fatal编程技术网

MySQL:在连接条件中使用别名

MySQL:在连接条件中使用别名,mysql,sql,Mysql,Sql,我正在尝试在加入条件中使用别名配置文件\u 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_coulmn LEFT JOIN

我正在尝试在加入条件中使用别名配置文件\u 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_coulmn
LEFT JOIN table3 AS t3
    ON t1.id = t3.some_coulmn
LEFT JOIN table4 AS t4
    ON profile_id = t4.some_column

重复条件中的表达式。我这样做了,查询继续进行,永远不会完成。可能其他未提交更改的事务会阻止它。或者,由于数据量大和/或索引不良,这需要很长时间。不过,别名在联接条件下不可用与此无关。它会失败并显示错误消息吗?@stickybit你说得对,只是花了太长时间,但它起了作用。
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