小编典典

MySQL Update查询具有多个值

sql

我在数据库中有一个表,记录如下:

match_id | guess | result
   125   |   1   |   0
   130   |   5   |   0
   233   |   11  |   0
   125   |   2   |   0

我的用户为每个匹配项选择一个猜测,我有一个函数可以根据匹配结果计算猜测的结果:如果猜测正确,则结果为(1)如果错误,则结果为(2
),如果比赛还没有结束,结果将是(默认为0),例如,我有11种猜测的可能性(一次可能有多个正确的猜测):如果我有一场比赛,id =
125,我拥有全部除了8,11之外的其他猜测都是错误的,因此我应该为具有匹配ID的所有匹配更新结果字段,并且其猜测为8或11(我将为此结果字段提供1),而我想为其他字段给出(2)猜对相同的比赛

我将此查询用于所有十一种可能性,如下所示:

UPDATE `tahminler` SET result=1 WHERE match_id='1640482' AND tahmin='8'
UPDATE `tahminler` SET result=1 WHERE match_id='1640482' AND tahmin='11'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='1'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='2'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='3'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='4'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='5'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='6'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='7'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='9'
UPDATE `tahminler` SET result=0 WHERE match_id='1640482' AND tahmin='10'

我想知道是否可以在一个查询中完成这项工作吗?


阅读 303

收藏
2021-04-22

共1个答案

小编典典

使用以下两个查询:

UPDATE `tahminler` 
        SET result=0 
        WHERE match_id='1640482' 
              AND tahmin IN ('1','2','3','4','5','6','7','9','10')

然后使用:

UPDATE `tahminler` 
        SET result=1 
        WHERE match_id='1640482' 
              AND tahmin IN ('8','11')
2021-04-22