0

i try to execute this query:

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

But i have this error:

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

i understand the error but not why come out. P.id not work?

2
  • You can't use a subquery yielding multiple columns that way. Try selecting only the ID field. Commented Nov 5, 2014 at 14:58
  • (I.e. select id from tab1 ...). Commented Nov 5, 2014 at 14:59

2 Answers 2

1

Change your query to:-

SELECT * 
FROM Tab1 P, Tab2
WHERE tab2.tab1_id=P.id
AND tab2.attr = "something"
AND tab1.type != "firstype"
Sign up to request clarification or add additional context in comments.

1 Comment

ok but P.id not work, so if i try with: from tab1,tab2 where tab2.tab1_id=tab1.id... works
0

You have the from clause in the wrong place. And, you should use explicit join syntax. Among other things, it will help prevent this type of error:

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

You also don't need the subquery. The filtering conditions can go in the on or in a where clause on the outer query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.