简体   繁体   中英

"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?

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
                 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM