我把union
放在两个select查询中,但是它仍然返回两行,它应该返回1行,因为我对它使用了union。为什么它返回两个不同的行?它应该返回单行而不是2行。
查询
ALTER PROCEDURE [dbo].[usp_GetUserProject]
(
@User_ID int=2
)
AS
BEGIN
if @User_ID !=0
begin
select distinct (x.Project_ID),x.ProjectName,x.ID,x.IsSource,x.IsDeployement
from
(
select p.Project_ID, cp.ProjectName
,ISNULL(pa.IsSource,'false') as IsSource
,isnull(pa.IsDeployement,'false') as IsDeployement
,ISNULL(pa.ID,0) AS ID
from Project p
inner join ProjectAssigment pa
on p.Project_ID =pa.Project_ID
--and p.UserID=pa.User_ID
inner join WebUsers u
on p.UserID=u.UserID
inner join ClientProjects cp
on cp.ClientProjectID=p.Project_ID
where pa.User_ID=@User_ID
--and pa.IsSource=1 and pa.IsDeployement=1
union
select p.Project_ID, cp.ProjectName
,ISNULL(pa.IsSource,'false') as IsSource
,isnull(pa.IsDeployement,'false') as IsDeployement
,ISNULL(pa.ID,0) AS ID
from Project p
inner join ClientProjects cp
on cp.ClientProjectID=p.Project_ID
left join ProjectAssigment pa
--on pa.Project_ID=p.Project_ID
on pa.User_ID=p.UserID
left join WebUsers u
on pa.User_ID=u.UserID
and u.UserID!=@User_ID
) as x
group by x.Project_ID,x.ProjectName,x.ID,x.IsDeployement,x.IsSource
end
END
发布于 2014-08-11 22:12:57
如果两个select查询的值不同,它将返回两行--如果相同,则在使用UNION
时返回单行。UNION
删除集合中的重复项,执行set联合操作。
您需要使用UNION ALL
https://stackoverflow.com/questions/25257255
复制相似问题