0

I'm trying to update 2 rows at once, I want to:

1) Set coupon.start (datetime) to the original account.vipExpires (datetime)

2) Add coupon.days (int) to account.vipExpires (datetime)

3) Set coupon.end (datetime) to the new value of account.vipExpires after coupon.days have been added

UPDATE accounts a, coupons c
SET c.start = a.vipExpires,
 a.vipExpires = DATE_ADD(a.vipExpires, INTERVAL c.days DAY),
 c.end = a.vipExpires
WHERE a.id = 1 AND c.id = 1;

The result I'm getting is:

c.start, a.vipExpires, c.end ALL end up as the new datetime with the added days.

How do I make c.start be equal to the original a.vipExpires value?

I originally thought the SET operations would execute in the order specified, it seems I was wrong.

asked May 9, 2020 at 11:54

1 Answer 1

1

Test

UPDATE coupons c, accounts a
SET c.start = a.vipExpires,
 c.end = DATE_ADD(a.vipExpires, INTERVAL c.days DAY),
 a.vipExpires = c.end
WHERE a.id = 1 AND c.id = 1;
answered May 9, 2020 at 19:47
2
  • Worked brilliantly, thanks! Still wonder why how SET precedence works though, or what was going on there. Commented May 9, 2020 at 20:10
  • @Smeagel the tables are updated in the order they're mentioned in UPDATE clause. Commented May 10, 2020 at 17:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.