我的表结构如下
主业务表(tab_operation)
ID (PK,int,not null)
ID_operation_sort(int) //单据性质
ID_company (int) //发生关系的公司
Income (money) //销售/折款收入
ID_transport_company(int) //货运公司
Other (nvarchar(50)) //备注
公司表(tab_company)
ID (PK,int,not null)
CompanyName (nvarchar(50))
目前的问题是ID_company和ID_transport_company都要参考公司表tab_company
结果CompanyName重复
以前的SQL语句(报错)
select * from tab_operation
Inner Join tab_company on tab_operation.ID_company=tab_company.ID
Inner Join tab_company on tab_operation.ID_transport_company=tab_company.ID
------解决方案--------------------------------------------------------
select A.*,B.CompanyName AS 关系的公司 ,C.CompanyName AS 货运公司 from tab_operation AS A Inner Join tab_company AS B on A.ID_company=B.IDInner Join tab_company AS C on A.ID_transport_company=C.ID
------解决方案--------------------------------------------------------
惭愧..
select a.*,fs_company=b.CompanyName,hy_company=c.CompanyName from tab_operation AS A
Inner Join tab_company AS B on A.ID_company=B.ID
Inner Join tab_company AS C on A.ID_transport_company=C.ID
------解决方案--------------------------------------------------------
为什么不在设计器里面拖放生成呢? 这样的语句完全可以自动生成,该别名自然微软会替你别名