Mysql 如何根据“时间”字段中的值添加新列`

Mysql 如何根据“时间”字段中的值添加新列`,mysql,Mysql,我有一张桌子: +--------+--------------+-----------+ | person |.....DAY......| ....hours | | Name1 ....2014-01-08... 03:53:23 | | Name2 ....2014-01-08... 05:30:54 | +--------|--------------|-----------+ 如何根据时间字段中的值添加新列标签? 如果时间从05:00:00到11:00:00

我有一张桌子:

+--------+--------------+-----------+   
| person |.....DAY......| ....hours |  
| Name1 ....2014-01-08... 03:53:23  |   
| Name2 ....2014-01-08... 05:30:54  |  
+--------|--------------|-----------+
如何根据时间字段中的值添加新列
标签
? 如果时间从05:00:00到11:00:00,则值将为
'1'
,否则
'0'
。它应该是这样的:

+---------+--------------+-----------+---------+  
| person  |.....DAY......| ..hours  |  label   |   
| Name1 ....2014-01-08... 03:53:23  |  0 ......|   
| Name2 ....2014-01-08... 05:30:54  |  1.......|   
+---------+--------------+----------+----------+

你可以用这样的东西

update table set label = case when hour(hours) between 5 and 10 then 0 else 1 end;
SELECT person, 
       DATE( dt ) AS DAY , 
       case when hour(from_unixtime(sec_to_time(SUM( IF( source LIKE '%вход' OR source LIKE '%въезд', -1, 1 ) * UNIX_TIMESTAMP( dt ) )))) between 5 and 11 
            then 1
            else 0 
       end AS hours
from your_table

你可以用这样的东西

update table set label = case when hour(hours) between 5 and 10 then 0 else 1 end;
SELECT person, 
       DATE( dt ) AS DAY , 
       case when hour(from_unixtime(sec_to_time(SUM( IF( source LIKE '%вход' OR source LIKE '%въезд', -1, 1 ) * UNIX_TIMESTAMP( dt ) )))) between 5 and 11 
            then 1
            else 0 
       end AS hours
from your_table

因为这是一个计算列,所以您可以像这样在查询中动态生成它

update table set label = case when hour(hours) between 5 and 10 then 0 else 1 end;
SELECT person, 
       DATE( dt ) AS DAY , 
       case when hour(from_unixtime(sec_to_time(SUM( IF( source LIKE '%вход' OR source LIKE '%въезд', -1, 1 ) * UNIX_TIMESTAMP( dt ) )))) between 5 and 11 
            then 1
            else 0 
       end AS hours
from your_table

因为这是一个计算列,所以您可以像这样在查询中动态生成它

update table set label = case when hour(hours) between 5 and 10 then 0 else 1 end;
SELECT person, 
       DATE( dt ) AS DAY , 
       case when hour(from_unixtime(sec_to_time(SUM( IF( source LIKE '%вход' OR source LIKE '%въезд', -1, 1 ) * UNIX_TIMESTAMP( dt ) )))) between 5 and 11 
            then 1
            else 0 
       end AS hours
from your_table

如果我的列
hours
在SELECT中声明,该怎么办:选择person,DATE(dt)作为DAY,sec_to_time(SUM)(如果(
source
LIKE“%oföззззз”或
source
LIKE“%ofъзззз”,“1)*UNIXзu时间戳(dt))作为小时)如果我的列
hours在SELECT:SELECT中声明,该,日期(dt)为天,秒到时间(总和(如果(
源代码
如“%БСз”或
源代码
如“%Съззз”,-1,1)*UNIX_时间戳(dt))为小时