两列连接到同一个表的同一列,怎么写sql查询语句?

表product中有一列incurrency表示商品进货的货币种类,另一列outcurrency表示货品卖出的货币种类,都是货币的索引。 表currency中有货币的索引。 如果我想要直接把进货卖货的货币都检索出来,要怎么写sql查询语句呢? 我原本是这样写的:

select currency.name as incurrencyname, currency.name as outcurrencyname
from product
inner join currency 
on product.in_currency = currency.id 
on product.out_currency = currency.id 

但是查询的时候会显示:

ERROR: syntax error at or near "on"
阅读 10.8k
1 个回答
select currency_in.name as incurrencyname, currency_out.name as outcurrencyname
from product
inner join currency as currency_in
on product.in_currency = currency_in.id
inner join currency as currency_out
on product.out_currency = currency_out.id
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题