简体   繁体   English

如果另一个表中的值相等,则更新一个表

[英]Update one table if value is equal in other table

I may have tried to reach too high this time, but I hope this can be done. 这次我可能试图达到很高的水平,但是我希望可以做到。 I have a table called liga like this: 我有一个名为liga的表,如下所示:

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

Where uid is short for user id . uid用户ID的缩写。 Then I have a table called kamp2 like this: 然后我有一个名为kamp2的表,如下所示:

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

These data is submitted by the user (attached to the uid ). 这些数据由用户提交(附加到uid )。
Now, what I would like is a form, where I write the results of a soccermatch, something like: 现在,我想要的是一个表格,我在其中编写足球比赛的结果,例如:

<input type="text" name="k1">... and so on...

... and then I should write the correct results. ...然后我应该写正确的结果。 So, to my actual question: 因此,对于我的实际问题:
Let's say the first match (k1=the winner(1X2) and k1r=result) was 2-1, I would like the form to update the liga -table with something like: 假设第一场比赛(k1 =获胜者(1X2)和k1r =结果)为2-1,我希望表单使用以下内容更新liga -table:

If (k1 == 1 AND k1r == 2-1) UPDATE liga SET point = point + 5 WHERE uid = $uid;
else if 
(k1r == 2-1 AND k1 != 1) UPDATE liga SET point = point + 3 WHERE uid = $uid ;
else if 
(k1 == 1 AND k1r != 2-1) UPDATE liga SET point = point + 1 WHERE uid = $uid ;

But how is this possible? 但这怎么可能呢? Should I SELECT the kamp2 table first and then use it or maybe JOIN the two tables or how? 我应该先选择kamp2表,然后再使用它,还是应该将两个表JOIN If this question is too "big" to answer, just let me know :) I don't want you to give me the exact code, just to guide me :) Hope it's okay! 如果这个问题太大而无法回答,请告诉我:)我不想让您给我确切的代码,只是为了指导我:)希望没问题!

You can join the tables together and then do the update with CASE logic: 您可以将表连接在一起,然后使用CASE逻辑进行更新:

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 = $uid ;

you can't update the same record with condition of that as well. 您也不能在相同条件下更新同一条记录。

You need to use sub query to update that. 您需要使用子查询来更新它。

UPDATE data_table t, (SELECT DISTINCT ID, NAME, VALUE FROM data_table WHERE VALUE IS NOT NULL AND VALUE != '') t1 SET t.VALUE = t1.VALUE WHERE t.ID = t1.ID AND t.NAME = t1.NAME t1 SET t.VALUE = t1.VALUE其中t.ID = t1.ID和t.NAME = t1.UPDATE data_table t,(从DISCTION_DISPLAY ID,NAME,值从data_table的值不为NULL和VALUE!='')名称

You can either use the concept of triggers to achieve this task: 您可以使用触发器的概念来完成此任务:

https://www.sitepoint.com/how-to-create-mysql-triggers/ https://www.sitepoint.com/how-to-create-mysql-triggers/

You'll have to create an after Update/insert trigger on kamp2 table and in that trigger you'll update liga table 您必须在kamp2表上创建更新/插入后触发器,然后在该触发器中更新liga

Or else handle this by backend php script. 否则通过后端php脚本处理此问题。

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

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