doulai2573 2017-04-06 05:47
浏览 217
已采纳

CASE中的多个WHERE条件

Yesterday, a member of stackoverflow helped me change a table in my database with a case from another table. But I need to input one more condition, and don't know where to put it. I have theese two tables:
liga

uid  |  name  |  games  |  points  |
___________________________________
1    | Daniel |    0    |    0     |
2    | Mikkel |    0    |    0     |

and kamp2:

uid  |  k1    |  k1r    |  k2   |  k2r   |  week  |
__________________________________________________
1    |  1     |  2-1    |  X    |  2-2   |  14    |
2    |  2     |  1-1    |  1    |  2-1   |  14    |

To those who haven't got it yet, it's a betting-game, where the user bets on 2 matches (k1 and k2) and a result on both (k1r and k2r). Let's say the first match is 2-1, i use:

UPDATE liga l JOIN
       kamp2 k
       ON l.uid = k.uid
    SET point = (CASE WHEN k.k1 = '1' and k.k1r = '2-1' THEN point + 5
                      WHEN k.k1r = '2-1' AND k.k1 <> '1' THEN point + 3 
                      WHEN k.k1 = '1' AND k.k1r <> '2-1' THEN point + 1 
                      ELSE point
                 END)
    WHERE l.uid = k.uid ;

and it workes perfectly. Daniel gets 5 points and Mikkel gets 3 points. But now to my problem. Next week they will send in another bet with the week-number 15. So I actually just want it to

UPDATE liga l JOIN
       kamp2 WHERE k.week = 15
       ......

but it won't allow that. Where should I put the condition then?

  • 写回答

2条回答 默认 最新

  • douzuo5504 2017-04-06 05:53
    关注

    You should add the where clause to the end (where the other where clause is located).
    In sql you first state which tables you like to update UPDATE table, then which fields you want to update with what values SET field='val' and lastly on which records this should be done WHERE field=42.

    UPDATE liga l JOIN kamp2 k ON l.uid = k.uid 
      SET point = (CASE WHEN k.k1 = '1' and k.k1r = '2-1' THEN point + 5 WHEN k.k1r = '2-1' 
       AND k.k1 <> '1' THEN point + 3 WHEN k.k1 = '1' 
       AND k.k1r <> '2-1' THEN point + 1 ELSE point END) 
       WHERE l.uid = k.uid AND k.week=15 ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形