Search code examples
mysqlmysql-error-1241

MySQL error: Operand should contain 1 column(s)


I have this MySQL Query (working)

First query:

SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (

SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)

The MySQL query above is to find the user id from two table

Now I want to compared again using this second MySQL query (working)

Second query:

SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no = 08123456789
AND counter < publisher_amount

But when I join all the query like this:

SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (

SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)

AND (
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no =08123456789
AND counter < publisher_amount
)

I get this error:

Operand should contain 1 column(s)

Then, I try using 1 column, but the result is not what I wanted.

My question is How to join first and second query ? and produce no error.

I have tried google it and after many "try-and-error" this is the far I can get to make the query work.


Solution

  • I think you just miss an EXISTS on your second subquery. Anyway, if I understand your query correctly, I think you could write your query as this:

    SELECT
      u.id
    FROM
      users u inner join user_counter uc
      on u.id=uc.publisher_id
         and no=08123456789
         and counter < publisher_amount
    WHERE
      u.publisher_set = '1'
      AND u.publisher_status = '1'
      AND u.publisher_content != ''
      AND u.publisher_amount != '0'
      AND u.publisher_now < publisher_max