如何在SQL Server中使用相等和不相等的内容

时间:2014-09-12 07:36:22

标签: sql sql-server

我必须从三个表中选择值。第一个表格为sale_project_detail,第二个表格为design_project_detail,第三个表格为design_designer

sale_project_detail架构:

Id int,
Name nvarchar(50),
ShortDescription nvarchar(max)

design_project_assignment架构:

Id int,
DId int (It is foreign key of design_designer table),
SPId int (It is foreign key of sale_project_detail table),
AssignDateTime datetime,
DueDate date,
Status varchar(10)

design_designer架构:

Id int,
Name nvarchar(15)

现在,我必须从sale_project_detail

中选择完整的详细信息
where assign_project_detail.SPId <> sale_project_detail.Id, 

Select design_designer(Name) 
from design_designer 
where design_designer.Id = assign_project_detail.DId

我正在尝试下面的代码,但它返回了错误的结果。

我的代码:

SELECT
   sale_project_detail.*,
   design_project_assignment.*,
   design_designer.Name
FROM
   sale_project_detail,
   design_project_assignment,
   design_designer
WHERE
   NOT EXISTS(SELECT NULL 
              FROM design_project_assignment
              LEFT JOIN design_designer ON design_designer.Id = design_project_assignment.DId
              WHERE sale_project_detail.Id = design_project_assignment.SPId)

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个

SELECT
  *
FROM
   sale_project_detail as spd 
   inner join design_project_assignment dpa on dpa.spid <> spd.id
   inner join design_designer dd on dd.id = dpa.did;