【问题标题】:How to change the value of this field in all the records of this MySql table?如何更改这个 MySql 表的所有记录中该字段的值?
【发布时间】:2014-08-15 17:24:47
【问题描述】:

我有以下情况。

MySql 数据库中,我有一个 posts 表。此表包含以下 2 个字段:post_modifiedpost_date

对于 posts 表中存在的每条记录,我需要将 post_modified 字段的值设置为 post_date

我怎样才能对所有表记录进行查询?

Tnx

【问题讨论】:

  • 表示输出的每一行都应该有相同的数据?
  • 您至少应该尝试过阅读 MySQL 文档。

标签: mysql sql database rdbms


【解决方案1】:

这很简单:

UPDATE posts SET post_modified = post_date;

【讨论】:

  • @KM。我也会投票给你,在 15 秒内给出三个答案 :-)
  • 有趣的是,人们也为这种简单的事情寻求解决方案,他们称自己为程序员/开发人员:)
【解决方案2】:

这个查询应该可以完成这项工作:

UPDATE posts SET post_modified=post_date;

【讨论】:

    【解决方案3】:

    您只需使用 update 不带条件来更新所有记录:

    update posts set post_modified = post_date
    

    根据数据库中的设置,可能不允许没有条件的更新。然后你会添加一个虚拟条件来告诉数据库你实际上想要更改每条记录:

    update posts set post_modified = post_date where 1 = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 2022-01-15
      • 1970-01-01
      • 2012-09-14
      • 2023-01-27
      • 1970-01-01
      相关资源
      最近更新 更多