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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用