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' )");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助