MySQL - Operand should contain 1 column(s)
Asked Answered
E

12

132

While working on a system I'm creating, I attempted to use the following query in my project:

SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by,
    users.id AS posted_by_id
    FROM users
    WHERE users.id = posts.posted_by)
FROM topics
LEFT OUTER JOIN posts ON posts.topic_id = topics.id
WHERE topics.cat_id = :cat
GROUP BY topics.id

":cat" is bound by my PHP code as I'm using PDO. 2 is a valid value for ":cat".

That query though gives me an error: "#1241 - Operand should contain 1 column(s)"

What stumps me is that I would think that this query would work no problem. Selecting columns, then selecting two more from another table, and continuing on from there. I just can't figure out what the problem is.

Is there a simple fix to this, or another way to write my query?

Endpaper answered 26/12, 2012 at 22:8 Comment(0)
B
139

Your subquery is selecting two columns, while you are using it to project one column (as part of the outer SELECT clause). You can only select one column from such a query in this context.

Consider joining to the users table instead; this will give you more flexibility when selecting what columns you want from users.

SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
COUNT( posts.solved_post ) AS solved_post,
users.username AS posted_by,
users.id AS posted_by_id

FROM topics

LEFT OUTER JOIN posts ON posts.topic_id = topics.id
LEFT OUTER JOIN users ON users.id = posts.posted_by

WHERE topics.cat_id = :cat
GROUP BY topics.id
Bioenergetics answered 26/12, 2012 at 22:10 Comment(3)
Thanks for the response. I'll fix my query, and mark you as the answer, but just for input, do you think there's a "better" way to write my query than what I'm using now (but also disregarding the error in it)?Endpaper
Ah. Thank you for the edit on your original post. I will be sure to mark you as the answer when StackOverflow lets me. Thanks a lot!Endpaper
Well, the COUNT() is throwing things off a bit; the query I've given will probably give an error due to the aggregation. You may need to move that aggregation into a subquery, depending on the goals of your query (which are not clear to me at the moment).Bioenergetics
F
34

In my case, the problem was that I sorrounded my columns selection with parenthesis by mistake:

SELECT (p.Column1, p.Column2, p.Column3) FROM Table1 p WHERE p.Column1 = 1;

And has to be:

SELECT p.Column1, p.Column2, p.Column3 FROM Table1 p WHERE p.Column1 = 1;

Sounds silly, but it was causing this error and it took some time to figure it out.

Frailty answered 7/9, 2020 at 15:15 Comment(3)
I can't believe this is what my problem was, I thought brackets were legal thereDominickdominie
Yes, this was my problem as well :) Thank youNeanderthal
Yes, same problem here. SQL and parenthesis and single quote marks around fields can be confusing.Intermolecular
T
24

This error can also occur if you accidentally use commas instead of AND in the ON clause of a JOIN:

JOIN joined_table ON (joined_table.column = table.column, joined_table.column2 = table.column2)
                                                        ^
                                             should be AND, not a comma
Turbit answered 1/4, 2016 at 6:29 Comment(1)
That really saved me a lot of time. Thanks!Difficile
O
17

This error can also occur if you accidentally use = instead of IN in the WHERE clause:

FOR EXAMPLE:

WHERE product_id = (1,2,3);
Osier answered 26/5, 2017 at 9:43 Comment(2)
Or LIKE instead of IN as i did and couldnt find why this error happens. ty for pointer.Universalism
It can also happen if you put brackets around the fields in the SELECT clause, e.g. SELECT (Field1, Field2) FROM TableValuation
T
7
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by,
    users.id AS posted_by_id
    FROM users
    WHERE users.id = posts.posted_by)

Well, you can’t get multiple columns from one subquery like that. Luckily, the second column is already posts.posted_by! So:

SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
posts.posted_by
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by_username
    FROM users
    WHERE users.id = posts.posted_by)
...
Tannen answered 26/12, 2012 at 22:10 Comment(0)
P
3

I got this error while executing a MySQL script in an Intellij console, because of adding brackets in the wrong place:

WRONG:

SELECT user.id
FROM user
WHERE id IN (:ids); # Do not put brackets around list argument

RIGHT:

SELECT user.id
FROM user
WHERE id IN :ids; # No brackets is correct
Pollypollyanna answered 15/10, 2020 at 13:28 Comment(3)
this does not match with the first answer here: #24551677Petit
@rubydio, that question references HQL and Hibernate, whereas my answer is referring to MySQL in an Intellij consolePollypollyanna
@janac-meena thanks, i knew it, but when is already written i couldn't see the errorShortwave
S
1

This error can also occur if you accidentally miss if function name.

for example:

set v_filter_value = 100;

select
    f_id,
    f_sale_value
from
    t_seller
where
    f_id = 5
    and (v_filter_value <> 0, f_sale_value = v_filter_value, true);

Got this problem when I missed putting if in the if function!

Salesgirl answered 21/8, 2019 at 13:4 Comment(0)
P
0

Another place this error can happen in is assigning a value that has a comma outside of a string. For example:

SET totalvalue = (IFNULL(i.subtotal,0) + IFNULL(i.tax,0),0)
Polivy answered 12/9, 2018 at 19:50 Comment(0)
P
0
(SELECT users.username AS posted_by,
users.id AS posted_by_id
FROM users
WHERE users.id = posts.posted_by)

Here you using sub-query but this sub-query must return only one column. Separate it otherwise it will shows error.

Prohibit answered 8/12, 2019 at 5:53 Comment(0)
G
0

I also have the same issue in making a company database. this is the code

SELECT FNAME,DNO FROM EMP 
 WHERE SALARY IN (SELECT MAX(SALARY), DNO
FROM EMP GROUP BY DNO);
Godden answered 1/10, 2022 at 13:39 Comment(0)
B
0

I also had this error message using Dapper with MySQL. Example code below:

SELECT ColA, ColB, ColC
FROM Table
WHERE ColA IN (@listOfColumns);

The issue with the above code is Dapper will automatically add the brackets for the @listOfColumns parameter substitution meaning the brackets in the snippet above will double wrap the list of columns causing the error.

Budde answered 12/4, 2023 at 5:46 Comment(0)
E
0

In my code, I wrote in this way before:

@Query(value = "SELECT pr.id"
    + " FROM biz_project pr"
    + " LEFT JOIN sys_user u ON pr.create_user_id = u.id"
    + " LEFT JOIN sys_department de ON pr.department_id = de.id"
    + " WHERE :roleUsers IS NULL OR pr.create_user_id IN (:roleUsers)"
    + " AND :roleDepts IS NULL OR pr.department_id IN (:roleDepts)", 
nativeQuery = true)
List<Long> test(@Param("userIdList") List<Long> userIdList,
    @Param("deptIdList") List<Long> deptIdList);

It has the same error, SQL Error: 1241, SQLState: 21000 Operand should contain 1 column(s).

But I'll remove these two parts:':roleUsers IS NULL OR ' and ':roleDepts IS NULL OR', It can run normally and return the result I want. But I don't know why.

Entablature answered 4/1 at 2:10 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewSculptor
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Arbuthnot

© 2022 - 2024 — McMap. All rights reserved.