duanboshe0001 2012-02-22 15:04
浏览 56
已采纳

使用当前行值mysql更新多行

I don't know if the title is clear but I'll try to explain it here. We have thousands of record for our database and there are a couple of datetime columns in theses tables. We have to move to int using unix_timestamp but I can't figure out a query in MySQL that would update these fields.

Let's say I have a simple table with a field like that :

user_table :
    id : int,
    name : string,
    date_joined : datetime;

To change all of these date_joined filed to a int I thought adding a int column and copying the date as a int into the new column then drop the old column and rename my new column could be a solution. I built a little php script my script will then make thousands of MySQL query which is quite long. My script would run queries like :

UPDATE user_table SET date_joined_int=UNIX_TIMESTAMP(date_joined) where id = 1
...

Is this possible to do this with one MySQL query?

  • 写回答

3条回答 默认 最新

  • doujue6196 2012-02-22 15:10
    关注

    You right, you should add new INT column

    ALTER TABLE user_table ADD date_joined_int INT(11) not null;
    

    Than convert your dates

    UPDATE user_table SET date_joined_int = UNIX_TIMESTAMP(date_joined);
    

    And finally remove date_joined column, and remane date_joined_int to date_joined

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题