我需要建议
Select *
From dbo.tblActivityDetails
WHERE (minWorked =0)
and Pillar = (
select distinct Pillar
from dbo.tblActivityDetails
where EmpID=0067
)
收到错误
“子查询返回的值超过1。当子查询跟随=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。”
如何编写查询以实现上述查询结果?
答案 0 :(得分:1)
这里你必须使用前1 ...因为distinct会给出不同的结果
Select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar =
(select top 1 Pillar from dbo.tblActivityDetails where EmpID=0067)
或使用Pillar(代替'=')期望多个值
Select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar in
(select distinct Pillar from dbo.tblActivityDetails where EmpID=0067)
答案 1 :(得分:1)
您可以使用IN
或EXISTS
Select * From dbo.tblActivityDetails d
WHERE (d.minWorked =0) and d.Pillar IN
(select d1.Pillar from dbo.tblActivityDetails d1 where d1.EmpID=0067)
Select * From dbo.tblActivityDetails d
WHERE (d.minWorked =0) AND
EXISTS (select 1 from dbo.tblActivityDetails d1
where d1.EmpID=0067 AND d.Pillar = d1.Pillar)
答案 2 :(得分:0)
;with cte As (select distinct Pillar
from dbo.tblActivityDetails
where EmpID=0067)
Select *
From dbo.tblActivityDetails As t
Join cte as c
On c.Pillar = t.pillar
WHERE minWorked = 0
这是我的头脑 - 直接打字,我可能会略微说法,但试着看看:)
答案 3 :(得分:0)
您的内部查询可以返回多行,因此您必须使用IN关键字而不是=运算符 解决方案
select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar in (select distinct Pillar from dbo.tblActivityDetails where EmpID=0067)
答案 4 :(得分:0)
为什么要使用子查询...只需转到
Select *
From dbo.tblActivityDetails
WHERE minWorked =0 and EMPID=0067

或者只是去
Select *
From dbo.tblActivityDetails
WHERE (minWorked =0)
and Pillar in (
select distinct Pillar
from dbo.tblActivityDetails
where EmpID=0067
)