简体   繁体   中英

A conditional SQL update statement for two tables at once

I want to update two tables at once. The code below seems to work fine. However, in some cases, there is no entry in bidGroups which means the entire statement would fail. How can I adjust it so it updates the first bit (watchedItems) and does not try the second part if watchedItems.bidGroupID IS NULL

UPDATE watchedItems, bidGroups 
SET watchedItems.won=1, bidGroups.bidGroupQty=bidGroups.bidGroupQty-1 
WHERE watchedItems.id=2 
AND watchedItems.aid=200618152822 
AND bidGroups.bidGroupID=watchedItems.bidGroupID 
AND bidGroups.id=2;

I tried this, but the syntax is wrong..

UPDATE  watchedItems, bidGroups 
SET watchedItems.won=1, 
CASE WHEN watchedItems.bidGroupID IS NOT NULL THEN bidGroups.bidGroupQty=bidGroups.bidGroupQty-1 
ELSE END
WHERE watchedItems.id=2 
AND watchedItems.aid=200618152822 
AND bidGroups.bidGroupID=watchedItems.bidGroupID 
AND bidGroups.id=2

You need to do a left outer join is all.

update watchedItems wi left outer join bidGroups bg
 on wi.bidGroupID = bg.bidGroupID
 set
  wi.won = 1,
  bg.bidGroupQty = bg.bidGroupQty - 1
 where wi.id = 2
 and wi.aid = 200618152822
 and (bg.id = 2 or bg.id is null)

If watchedItems.bidGroupId is null, then it will only update the found row in watchedItems , since there's nothing to join to in bidGroups . If it's not and the join gets rows in both tables, the updates to both tables happen just fine.

Just using watchedItems, bidGroups is the same as inner join , so when there isn't an entry in bidGroups to join to, you also don't get a result from watchedItems . Use the same join and where clauses on a select instead of an update , and you'll see the difference:

select wi.won, bg.bidGroupQty
 from watchedItems wi, bidGroups bg
 where wi.id = 2 and wi.aid = 200618152822
 and (bg.id = 2 or bg.id is null)
 and wi.bidGroupID = bg.bidGroupID

vs:

select wi.won, bg.bidGroupQty
 from watchedItems wi left outer join bidGroups bg
 on wi.bidGroupID = bg.bidGroupID
 where wi.id = 2 and wi.aid = 200618152822
 and (bg.id = 2 or bg.id is null)

Try LEFT JOIN this way:

UPDATE watchedItems 
LEFT JOIN bidGroups ON watchedItems.bidGroupID = bidGroups.bidGroupID AND bidGroups.id=2
SET watchedItems.won=1, bidGroups.bidGroupQty=bidGroups.bidGroupQty-1 
WHERE watchedItems.id=2 
AND watchedItems.aid=200618152822;

......

just set it to its current value if you dont want to change. And MySQL usually optimises it away by not updating it when the value is same.

UPDATE watchedItems 
  LEFT JOIN bidGroups ON bidGroups.bidGroupID=watchedItems.bidGroupID 
       AND bidGroups.id=2
SET watchedItems.won=1, 
    bidGroups.bidGroupQty= if(watchedItems.bidGroupID IS NULL, 
                              bidGroups.bidGroupQty, 
                              bidGroups.bidGroupQty-1)
WHERE watchedItems.id=2 
AND watchedItems.aid=200618152822;

however, you dont need the if because if watchedItems.bidGroupID IS NULL then row from bidGroups is not joined and update for that non-existant row is ignored. So bidGroups.bidGroupQty=bidGroups.bidGroupQty-1 is just fine

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