我有大量的MySQL记录数据,必须像这样进行转换:
我的问题是,如何提取日期时间值(月,周,小时)并将其插入到具有二进制值(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
;
答案 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
;