Memphis Meng
Memphis Meng

Reputation: 1681

"Scalar subquery produced more than one element"

I need to update a column's value by simply inserting a column of values according to another table in this way:

UPDATE `inbound-summit-278521.MET689.fact_posts`
SET `inbound-summit-278521.MET689.fact_posts`.accepted = 
(SELECT (CASE WHEN id IN (SELECT T2.id
FROM `inbound-summit-278521.MET689.fact_posts` T1
JOIN `inbound-summit-278521.MET689.fact_posts` T2
ON T1.accepted_answer_id = T2.post_id)
THEN 1 ELSE 0 END)
FROM `inbound-summit-278521.MET689.fact_posts`)
WHERE `inbound-summit-278521.MET689.fact_posts`.accepted IS NULL;

The output error is shown as the title goes but no syntax error was detected. What did it go wrong? How should I fix it?

Upvotes: 0

Views: 197

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270081

It is really hard to tell what you are trying to do. It seems to be that you want to set a flag to 1 if a corresponding row exists in the same table based on an "answer".

If so, you can use a correlated subquery. Something like this:

UPDATE `inbound-summit-278521.MET689.fact_posts` fp
    SET accepted = 1
    WHERE fp.accepted IS NULL AND
          EXISTS (SELECT 1
                  FROM `inbound-summit-278521.MET689.fact_posts` fpa
                  WHERE fpa.accepted_answer_id = fp.post_id
                 );

Upvotes: 1

Related Questions