获取其他表中没有记录的所有对象

时间:2017-04-24 07:53:00

标签: sql-server

我对sql很新,我试图获得所有其他表中没有记录的项目。例如;有许多项目和项目有很多来源,但有些项目根本没有任何来源。我想只检索在另一个表中没有任何记录的项目...这是我尝试的代码,但它仍然给了我一些有源代码的项目。任何人都可以帮我这个吗?谢谢!

SELECT        
                    p_Project.Project_ID as Id
                  , p_Project.ProjectNum
                  , p_Project.ProjectName
                  , p_Project.Client_ID 
                  , p_Project.Industry
                  , p_Project.ProjectStatus
                  , p_Project.EffectiveDate
                  , p_Project.ProjectOrigination
                  , p_Project.ProjDateClosed
                  , p_Project.PrimaryMaterial
                  , p_Project.PrimaryService
                  , p_Project.PrimarySource
                  , p_Project.ProjectCategory
                  , p_Client.Client_ID as Id
                  , p_Client.ClientName
            FROM  p_Project
                LEFT OUTER JOIN
                     p_Client ON p_Project.Client_ID = p_Client.Client_ID 
            WHERE p_Project.Project_ID IN (  SELECT p_Project.Project_ID
                                 FROM  p_Project
                                 LEFT OUTER JOIN p_Client ON p_Project.Client_ID = p_Client.Client_ID WHERE p_Project.Project_ID IN (SELECT p_Project.Project_ID as Id FROM p_Project   WHERE  p_Project.ProjectStatus  ='active') AND p_Project.Project_ID IN (SELECT p_Project.Project_ID as Id FROM p_Project LEFT OUTER JOIN p_Project_Pricing ON p_Project.Project_ID = p_Project_Pricing.Project_ID  WHERE (  p_Project_Pricing.SourceCode  IS NULL OR  p_Project_Pricing.SourceCode  LIKE N'') AND ( p_Project.PrimarySource  IS NULL OR p_Project.PrimarySource  LIKE N''))
                                GROUP BY p_Project.Project_ID, p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum
                                ORDER BY p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum
                                OFFSET (0) ROWS FETCH NEXT (25) ROWS ONLY )

1 个答案:

答案 0 :(得分:1)

尝试以下内容。

select proj.* from [yourprojtable] proj
left join [yoursourcetable] src
on proj.projid = src.projid
where src.projid is null

在您的查询中尝试将where条件更改为“P_Client.Client_id为空”。