2014-11-05 42 views
0

我試圖執行這個查詢:嵌套在SQL錯誤操作數選擇應包含1列

select *, (select * from tab1 where tab1.type!="firstype") as P 
from tab2 where tab2.attr="something" and tab2.tab1_id=P.id 

但我有此錯誤:

Error Code: 1241. Operand should contain 1 column(s) 0,001 sec 

我理解的錯誤,但不知其所以然出來。 P.id不工作?

+0

不能使用子查詢產生的多個列的方式。嘗試只選擇ID字段。 – 11684 2014-11-05 14:58:28

+0

(即「從tab1 ...中選擇ID」)。 – 11684 2014-11-05 14:59:07

回答

1

查詢更改爲: -

SELECT * 
FROM Tab1 P, Tab2 
WHERE tab2.tab1_id=P.id 
AND tab2.attr = "something" 
AND tab1.type != "firstype" 
+0

確定,但P.id不工作,所以如果我嘗試:從tab1,tab2其中tab2.tab1_id = tab1.id ...作品 – 2014-11-05 15:07:58

+0

是的,它肯定會工作。 – 2014-11-05 19:45:22

0

你在錯誤的地方有from子句。而且,您應該使用明確的join語法。除此之外,它將有助於防止此類錯誤:

select * 
from tab2 join 
    tabl p 
    on tab2.tab1_id = P.id and 
     tab2.attr = 'something' and 
     p.type <> 'firstype'; 

您也不需要子查詢。篩選條件可以在外部查詢的onwhere子句中進行。

相關問題