不了解sql连接上的相关名称

时间:2014-10-28 21:08:54

标签: sql sql-server database join sql-server-2012

有人可以向我解释如何在此联接中关联这些名称吗?当我执行时,我得到错误,说明From子句中的对象具有相同的暴露名称

select
inv_mast.item_id as Item,
inv_loc.location_id as location,
company.company_name as company,
vendor_supplier.vendor_id as vendorID
from
inv_loc
join inv_mast on inv_mast.inv_mast_uid = inv_loc.inv_mast_uid 
join inv_loc on company.company_id = inv_loc.company_id 
JOIN company ON (company.company_id = vendor_supplier.company_id) 

1 个答案:

答案 0 :(得分:1)

您加入inv_loc两次 - 您需要修复联接或引入别名,如下所示:

select      inv_mast.item_id as Item,
            mast.location_id as MasterLocation,
            companylocation.location_id as CompanyLocation,
            company.company_name as company,
            vendor_supplier.vendor_id as vendorID

from        inv_loc AS mast

join        inv_mast 
    on      inv_mast.inv_mast_uid = mast.inv_mast_uid 

join        inv_loc AS companylocation
    on      company.company_id = companylocation.company_id 

JOIN        company 
    ON      company.company_id = vendor_supplier.company_id