5

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

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

我的用户为每场比赛选择一个猜测,我有一个函数可以根据匹配结果计算猜测的结果:如果猜测正确,结果将为(1)如果错误,结果将为(2 )如果比赛还没有完成,结果将是(0默认)我有十一种猜测的可能性(不止一个可能同时是正确的)例如:如果我有一个匹配 whit 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'

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

4

3 回答 3

5

使用这两个查询:

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')
于 2013-11-01T13:02:01.153 回答
4

你可以这样做,但它也会很丑陋。使用CASE()运算符,例如:

UPDATE tahminler
SET
  result=CASE
    WHEN tahmin IN ('1','2','3','4','5','6','7','8','9','10') THEN 0
    WHEN tahmin IN ('8', 11) THEN 1
  END
WHERE
  match_id='1640482'
于 2013-11-01T13:05:05.653 回答
1
UPDATE `tahminler` SET `result` = CASE WHEN tahmin IN(8,11) THEN 1
                                   WHEN tahmin IN(2,3,4,5,6,7,9,10) THEN 0
                              END
 WHERE match_id = 1640482;
于 2013-11-01T13:07:15.577 回答