不是唯一的表/别名

时间:2017-09-05 15:21:10

标签: mysql

所以我尝试使用多个连接运行此查询以获得我想要的确切行但我一直收到此错误

  

不是唯一的表/别名:' ss_prices'

我正在运行的查询:

select `ss_accounts`.`id`, 
       `ss_accounts`.`bot_acc_id`, 
       `ss_accounts_inventories`.*, 
       `ss_prices`.* 
    from `ss_accounts_inventories` 
        left join `ss_prices` 
            on `ss_accounts_inventories`.`item_name` = `ss_prices`.`item_name` 
        left join `ss_prices` 
            on `ss_accounts_inventories`.`phase` = `ss_prices`.`item_phase` 
        inner join `ss_accounts` 
            on `ss_accounts_inventories`.`bot_id` = `ss_accounts`.`id` 
    order by `ss_prices`.`item_price` DESC

1 个答案:

答案 0 :(得分:3)

你要加入同一张桌子两次:<​​/ p>

...
left join `ss_prices` on ...
left join `ss_prices` on ...
...

当您引用该表时,查询中的任何其他位置,查询引擎无法知道您指的是哪一个。例如,在select子句中:

`ss_prices`.*

为每个连接的表添加一个别名,以便区分它们:

...
left join `ss_prices` as item_name_prices on ...
left join `ss_prices` as phase_prices on ...
...

然后在查询的其余部分中通过别名引用它们:

select
  `ss_accounts`.`id`,
  `ss_accounts`.`bot_acc_id`,
  `ss_accounts_inventories`.*,
  `item_name_prices`.*, -- here
  `phase_prices`.* -- and here, etc.