简体   繁体   English

一次两个表的条​​件SQL更新语句

[英]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. 但是,在某些情况下,bidGroups中没有条目,这意味着整个语句将失败。 How can I adjust it so it updates the first bit (watchedItems) and does not try the second part if watchedItems.bidGroupID IS NULL 如何调整它以便更新第一位(watchedItems)并且如果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. 你需要做一个left outer join

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 . 如果watchedItems.bidGroupId为空,那么它只会更新在发现的行watchedItems ,因为没有什么可以加入到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 . 仅使用watchedItems, bidGroupsinner join watchedItems, bidGroups相同,因此当bidGroupsbidGroups要加入的条目时,您也无法从watchedItems获得结果。 Use the same join and where clauses on a select instead of an update , and you'll see the difference: select而不是update上使用相同的joinwhere子句,您将看到差异:

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: 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: 以这种方式尝试LEFT JOIN:

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. 并且MySQL通常在值相同时不更新它来优化它。

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. 但是,您不需要if因为如果watchedItems.bidGroupID IS NULL则不会加入bidGroups中的行,并忽略该不存在的行的更新。 So bidGroups.bidGroupQty=bidGroups.bidGroupQty-1 is just fine 所以bidGroups.bidGroupQty=bidGroups.bidGroupQty-1就好了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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