您好我有一个包含以下列的MySql表;
Year Month Day Time Temp
------------------------
2014 12 30 23.5 15.9
2014 12 31 00.0 13.2
2014 12 31 00.5 11.5
以上数据为30分钟(半小时记录)。
Structure:
----------
Year = int(4)
Month = int(2)
Day = int(2)
Time = decimal(3,1) "decimal 24hrs"
Temp = decimal(3,1)
我需要查询表返回;
Timestamp Date/Time and Temp;e.g.
---------------------------------
2014-12-30 23:30:00 15.9
2014-12-31 00:00:00 13.2
2014-12-31 00:30:00 11.5
我可以返回Date部分(和temp),但无法转换/包含时间。
我目前正在使用;
SELECT concat(`Year`,'-',`Month`,'-',`Day`) AS `MyDateTimeStamp`, `Temp`
FROM `MyTable'
我搜索了线程,找不到类似的例子。 先感谢您, 问候 托尼
答案 0 :(得分:0)
尝试使用IF()
函数和FLOOR()
函数,例如:
SELECT concat(`Year`,'-',`Month`,'-',`Day`,' ',
FLOOR(`Time`), IF((`Time`/FLOOR(`Time`))>1,' :30:00',' :00:00')) AS `MyDateTimeStamp`, `Temp`
FROM `myTable`
请参阅Demo。
答案 1 :(得分:0)
要转换时间,请使用sec_to_time()
。您需要将该时间转换为秒。
示例: 23.5
相当于23:30:00
,因此,23.5 / 24
是相当于该时间的一天中的一小部分。所以:(23.5 / 24) * 24 * 60 * 60 = 23.5 * 60 * 60
是秒数。最后:
select sec_to_time(23.5 * 60 * 60);
-- Result: 23:30:00
所以,要获得日期/时间:
select concat(concat_ws('-', `year`, `month`, `day`), ' ', sec_to_time(`time` * 60 * 60));