LEFT JOIN别名的未知列错误

时间:2012-08-23 12:08:52

标签: mysql

我试图在左连接中添加别名,以便我可以在同一个表(不同的键)上进行两次连接,但是遇到了这个问题。我已经将问题简化为问题所在,以保持简单。

SELECT stock_items.id AS stockid, exchange_rates.rate AS rate
FROM stock_items
LEFT JOIN exchange_rates AS exr ON stock_items.currency = exchange_rates.id

当我运行此查询时,我得到了

1054 - 'on clause'

中的未知列'exchange_rates.id'

但是,如果我删除了别名,那么它就像这样...

SELECT stock_items.id AS stockid, exchange_rates.rate AS rate 
FROM stock_items 
LEFT JOIN exchange_rates ON stock_items.currency = exchange_rates.id

此查询运行正常......?

1 个答案:

答案 0 :(得分:1)

如果您添加alias,则应在join

中使用它
SELECT stock_items.id AS stockid, exr.rate AS rate
FROM stock_items
LEFT JOIN exchange_rates exr ON stock_items.currency = exr.id