如何使用别名和条件连接2个表

时间:2015-01-15 17:18:47

标签: sql sql-server join

表1:承包商

Contractor_No; -- PK
Contractor;

表2:合同

Contract_No;   -- PK
ProjDesc;
Contractor_No; -- FK

如何通过将这两个表与Contractor(来自表1),Contract_No,ProjDesc(表2)连接起来来检索数据?

我的SQL语句:

select a.Contractor, b.ProjDesc
from Contractors a, Contracts b
join Contractor a on a.Contractor_No = b.Contractor_No and b.Contract_No='1234';

2 个答案:

答案 0 :(得分:2)

您有一些语法错误:

SELECT a.Contractor
     , b.ProjDesc
FROM Contractors a 
JOIN Contracts b ON a.Contractor_No = b.Contractor_No 
                AND b.Contract_No = '1234'

答案 1 :(得分:1)

select a.Contractor,
       b.ProjDesc
from   Constractors a
       inner join contracts b on
        a.contractor_no = b.contractor_no
where  b.contract_no = '1234'