Home > Database > Mysql Tutorial > Why Does My MySQL Query Return 'Operand should contain 1 column(s)' and How Can I Fix It?

Why Does My MySQL Query Return 'Operand should contain 1 column(s)' and How Can I Fix It?

Susan Sarandon
Release: 2025-01-12 15:06:47
Original
1002 people have browsed it

Why Does My MySQL Query Return

MySQL query error: "Operand should contain 1 column"

Problem description:

While executing a MySQL query that joins multiple tables and contains a subquery (selecting columns from another table), an error occurred: "#1241 - Operand should contain 1 column(s)".

Query:

<code class="language-sql">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</code>
Copy after login

Error reason:

The

error occurs because the subquery selects two columns (users and username) from the id table, and the outer query expects the subquery to return only a single column.

Solution:

There are two ways to resolve this error:

  1. Modify subquery:

    • Modify the subquery to return only username columns or id columns.
  2. Connect users table:

    • Instead of using a subquery, join the posted_by table directly to the users table using the posts column. This approach provides more flexibility when selecting the required columns from the users table.

Modified query:

Use subquery:

<code class="language-sql">SELECT
topics.id,
topics.name,
topics.post_count,
topics.view_count,
COUNT( posts.solved_post ) AS solved_post,
(SELECT users.username AS posted_by
    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</code>
Copy after login

Use connection:

<code class="language-sql">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</code>
Copy after login

The above is the detailed content of Why Does My MySQL Query Return 'Operand should contain 1 column(s)' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template