MySql,将日期和时间列组合成时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4125931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql, combining date and time column into a time stamp
提问by Richard
I am guessing this is relatively simple to do, but I am unsure of the syntax. I have date and time columns that I want to combine to a timestamp column. how would I query this using a select?
我猜这相对简单,但我不确定语法。我有要合并到时间戳列的日期和时间列。我将如何使用选择查询这个?
回答by ConceptRat
Or you could try the built-in TIMESTAMP(date,time)
function.
或者你可以试试内置TIMESTAMP(date,time)
函数。
回答by Haim Evgi
Mysql does not seem to have a constructor for datetime such as datetime('2017-10-26', '09:28:00'). So you will have to treat the component part as string and use string concatenation function (Note mysql does not have the || operator for string concatenation). If you want the datetime type, you will have to cast it.
Mysql 似乎没有日期时间的构造函数,例如 datetime('2017-10-26', '09:28:00')。因此,您必须将组件部分视为字符串并使用字符串连接函数(注意 mysql 没有用于字符串连接的 || 运算符)。如果您想要日期时间类型,则必须对其进行转换。
concat(datefield,' ',timefield) as date
select cast(concat('2017-10-26', ' ', '09:28:00') as datetime) as dt;
回答by Tom Lime
If it possible to use built-in function, just use it. Any way here is an example to find records between given timestamps.
如果可以使用内置函数,就使用它。这里的任何方式都是在给定时间戳之间查找记录的示例。
SELECT `id` FROM `ar_time` WHERE TIMESTAMP(`cdate`,`ctime`) BETWEEN fromTimeStamp AND nowTimeStamp;
回答by Daniel James Canil
For 24hr time
24 小时
TIMESTAMP(Date, STR_TO_DATE(Time, '%h:%i %p'))
回答by Lydia
SELECT * FROM tablename WHERE TIMESTAMP(datecol, timecol) > '2015-01-01 12:00:00';
回答by Bob Stein
O.P. did say SELECT
but in case anyone wants to add a timestamp column:
OP 确实说过,SELECT
但如果有人想添加时间戳列:
ALTER TABLE `t` ADD COLUMN `stamp` TIMESTAMP;
UPDATE `t` SET `stamp` = STR_TO_DATE(CONCAT(`Date`, ' ', `Time`), '%m/%d/%Y %H:%i:%s');
Adjust format stringsto taste.
根据口味调整格式字符串。