duanpuchen3142 2014-08-16 05:37
浏览 9
已采纳

如何将两个mysql列一起添加,然后将结果更新为相同的列之一?

I want to add two columns together and then update one of those columns with the new result for each row every 5 seconds. (I know, probably not very practical in real life but it's for science!)

So this is what I figured out so far:

  1. In order to make the database update happen every 5 seconds, I would either have to run a CRON job or use mySQL's event creator (I decided to go with the mySQL one) link

  2. mySQL has something called SUM but it's really hard to understand and many of the answers are really just answers without any explanation :/

  3. I would also have to run an UPDATE on the previous query.

So my table would be this:

kingdom(table)
____________
kingdom_id(int) | kingdom_gold(int) | kingdom_gold_rate(int)
____________

The idea is that kingdom_gold(int) would add kingdom_gold_rate(int) every 5 seconds. So if kingdom_gold = 0 and kingdom_gold_rate = 10; after 5 seconds kingdom_gold would be updated to 10. 5 seconds later, 20 etc...

But this would have to apply to all rows in the kingdom table. I think I understand how mysql scheduled events work but as far as adding those two columns and then updating it for every row is really beyond my understanding and I'd like to ask you all to help me learn this. Thank you!

  • 写回答

2条回答 默认 最新

  • dskld5423 2014-08-16 05:40
    关注

    The query should be pretty simple. This ought to work:

    UPDATE kingdom 
    SET kingdom_gold = kingdom_gold + kingdom_gold_rate
    

    Note that by not specifying a WHERE clause, the UPDATE will apply to all table records

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?