MySQL - 提取日期时间并转换为矩阵映射

时间:2015-11-05 14:26:34

标签: mysql datetime

我有大量的MySQL记录数据,必须像这样进行转换:

Datetime matrix mapping

我的问题是,如何提取日期时间值(月,周,小时)并将其插入到具有二进制值(0或1)的精确列中,如上图所示。

问题已解决,我只是想出了最简单的方法,例如:

UPDATE TABLE
SET h1 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '1' THEN 1 
         ELSE '0'
         END
, h2 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '2' THEN '1' 
         ELSE '0' 
         END
, h3 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '3' THEN '1' 
         ELSE '0' 
         END
, h4 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '4' THEN '1' 
         ELSE '0' 
         END
, h5 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '5' THEN '1' 
         ELSE '0' 
         END
, h6 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '6' THEN '1' 
         ELSE '0' 
         END
, h7 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '7' THEN '1' 
         ELSE '0' 
         END
         ;

1 个答案:

答案 0 :(得分:0)

问题已解决,我只是想出了最简单的方法,例如:

UPDATE TABLE
SET h1 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '1' THEN 1 
         ELSE '0'
         END
, h2 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '2' THEN '1' 
         ELSE '0' 
         END
, h3 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '3' THEN '1' 
         ELSE '0' 
         END
, h4 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '4' THEN '1' 
         ELSE '0' 
         END
, h5 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '5' THEN '1' 
         ELSE '0' 
         END
, h6 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '6' THEN '1' 
         ELSE '0' 
         END
, h7 = CASE WHEN (SELECT DAYOFWEEK (trx_datetime)) = '7' THEN '1' 
         ELSE '0' 
         END
         ;