2017-05-31 83 views
-1

我需要SQL查詢中查詢與鮮明

Select * 
From dbo.tblActivityDetails 
WHERE (minWorked =0) 
    and Pillar = (
       select distinct Pillar 
       from dbo.tblActivityDetails 
       where EmpID=0067 
       ) 

得到錯誤的建議

「子查詢返回多個值。當子查詢遵循=,!=,<這是不允許的,< =,>,> =或者當子查詢用作表達式時。「

如何編寫查詢以實現上述查詢結果?

+0

做什麼,你的輸出從內選擇和整體的查詢期望。有幾種方法可以解決這個錯誤,但是要做的是取決於你想要從每個查詢中獲得什麼。 – MarkD

+0

您使用MySQL,MS SQL Server或SQlite嗎?不要標記不涉及的產品... – jarlh

回答

1

這裏u必須使用前1 ......因爲不同會給不同的結果

Select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar = 
(select top 1 Pillar from dbo.tblActivityDetails where EmpID=0067) 

或使用支柱(代替 '=')期待多個值

Select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar in 
    (select distinct Pillar from dbo.tblActivityDetails where EmpID=0067) 
+0

夢幻般的@Ravi,只是改變'='爲'在'解決。非常感謝。 – user3926897

+0

歡迎兄弟....玩得開心 – Ravi

+0

你甚至可以直接在主查詢中使用empid = 0067 ...不需要子查詢檢查 – Ravi

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 

這是關閉我的頭頂 - 直接輸入,我可能在語法上略微瘋狂,但嘗試看:) :)

+0

謝謝@rachel ambler – user3926897

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) 
+0

呵呵,甚至沒有注意到我自己:) –

+0

還有更多柱子如上查詢無濟於事 – user3926897

+0

爲什麼不能?我認爲這與使用'Pillar IN(...)'相同。你可以添加一個樣本以獲得更多詳細信息 – TriV

0

你的內部查詢可以返回多個行,以便代替=運算符你爲什麼要使用子查詢使用IN關鍵字 解決方案

select * From dbo.tblActivityDetails WHERE (minWorked =0) and Pillar in (select distinct Pillar from dbo.tblActivityDetails where EmpID=0067) 
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 
 
       )