【问题标题】:Update a new added column at once in mysql在mysql中一次更新一个新添加的列
【发布时间】:2017-10-12 17:57:53
【问题描述】:

我向名为salary 的当前表employee 添加了新列,现在我必须为100 条记录添加值。我不想写更新列 100 次,而且我无法定义 case 语句,因为每个员工 ID 的薪水是不同的。所以下面的命令不是我想要的。

     UPDATE employee
    SET salary = 10000
    WHERE employeeId = 1;

THE SAME FOR employeeId = 2,3,....

我很有趣将 id 作为@id 之类的参数,就像employeeId 定义的 aut_increment 一样,因此在每次迭代中为其添加相应的值。

想象工资是 (1000,2000,4000,5000,....) 最好的方法或解决方案是什么?

【问题讨论】:

  • @Strawberry 我无法理解你否定我的问题是什么意思?而且我在您在此处发布的链接中看不到我的答案。我是 mysql 新手,我在问一个问题,在这里看了 3 个小时并没有找到正确的答案,所以如果你知道一些事情,试着帮助别人而不是拒绝他们!
  • 如果您将鼠标悬停在向下箭头上,它可能会提供线索。

标签: mysql sql updates


【解决方案1】:

在spring jpa中,查找所有记录,遍历每条记录并进行更新操作。示例:

List<YourEntity> entities= repositoryObject.findAll();

for (Entity e : entities)
{
e.setSalary(<your salary values>). // salary values you can read from a file etc..

repositoryObject.save(e); //Do the save operation on the object you are looping through. Hence other column values still persist in the object. Hence you would update the row.

}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多