1

I have added the following query but it doesn't work, after I added set x line. Please help me fix this query

BEGIN
  DECLARE x INT;
  SET x = SELECT donation_id FROM wp_give_donationmeta ORDER BY wp_give_donationmeta.meta_id DESC LIMIT 1;

   UPDATE wp_posts SET post_status= 'Sponsored' WHERE ID= x;
END
2
  • What error you are gettting? Commented Aug 20, 2019 at 13:12
  • MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT donation_id FROM wp_give_donationmeta ORDER BY wp_give_donationmeta.meta_' at line 3 Commented Aug 20, 2019 at 13:15

2 Answers 2

1

Try to use EXISTS

BEGIN

   UPDATE wp_posts 
   SET post_status= 'Sponsored' 
   WHERE EXISTS( SELECT donation_id 
                 FROM wp_give_donationmeta 
                 WHERE wp_posts.ID = wp_give_donationmeta .donation_id);
END
Sign up to request clarification or add additional context in comments.

Comments

1

You could the update just using a single query

UPDATE wp_posts S
INNER JOIN (
  SELECT donation_id 
  FROM wp_give_donationmeta 
  ORDER BY wp_give_donationmeta.meta_id DESC 
  LIMIT 1
) t 
SET post_status= 'Sponsored' WHERE ID= t.donation_id;

assuming you are creating valid trigger then you could

BEGIN
  UPDATE wp_posts S
  INNER JOIN (
    SELECT donation_id 
    FROM wp_give_donationmeta 
    ORDER BY wp_give_donationmeta.meta_id DESC 
    LIMIT 1
  ) t 
  SET post_status= 'Sponsored' WHERE ID= t.donation_id;
END;

1 Comment

I need to add this query to trigger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.