dongzhuohan7085 2017-02-01 08:46
浏览 104
已采纳

Mysql搜索表查找datetime字段并更改时区

I have tables in my legacy database that stored dates in CST format. Now I want to store them in UTC.

I want to create a script/query, in mysql, that would search my database for tables with datetime fields and then convert the values there to UTC.

A script that would do this without having to write table names would be fun.

I have the query to convert time zone with CONVERT_TZ, that is not a problem with me. But I want a script that searches through the database for tables and fields that has datetime as data type and then run the script on those fields.

Any idea how I can do that? Any reference would be nice, doesn't have to be code.

  • 写回答

1条回答 默认 最新

  • drdyf42880 2017-02-01 09:11
    关注

    Fetch the columns first, traverse the array to decide if it matches the Type you want.

    Then make the update is just fine.

    <?php
    $db = new Mysqli("host", "user", "pass", "db");
    
    $column_query = $db->query("SHOW COLUMNS IN table");
    if ($column_query->num_rows != 0){
        while ($column = $column_query->fetch_object()){
            if ($column->Type != 'datetime')
                continue;
            $db->query("UPDATE table SET {$column->Field}=CONVERT_TZ({$column->Field}, 'timezoneFrom', 'timezoneTo' )");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备