MYSQL:如何使用月、日和年设置日期(makedate?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2437110/
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: How do I set a date (makedate?) with month, day, and year
提问by chongman
I have three columns, y, m, and d (year, month, and day) and want to store this as a date.
我有三列,y、m 和 d(年、月和日),并希望将其存储为日期。
What function would I use on mySQL to do this?
我会在 mySQL 上使用什么函数来做到这一点?
Apparently makedate uses year and day of year (see below), but I have month.
显然 makedate 使用年份和年份(见下文),但我有月份。
I know I can use STR_TO_DATE(str,format), by constructing the string from (y,m,d), but I would guess there is an easier way to do it.
我知道我可以通过从 (y,m,d) 构造字符串来使用 STR_TO_DATE(str,format),但我想有一种更简单的方法来做到这一点。
REFERENCES
参考
Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the result is NULL.
返回日期,给定年份和年份值。dayofyear 必须大于 0,否则结果为 NULL。
回答by Kaleb Brasee
I believe you can use a string in the proper format:
我相信您可以使用正确格式的字符串:
UPDATE table SET my_date = '2009-12-31';
Edit:Yeah you can, just verified it in MySQL 5.1.
编辑:是的,你可以,只是在 MySQL 5.1 中验证了它。
回答by josh
It isn't high on readability but the following would work:
它的可读性不高,但以下方法可行:
SELECT'1900-01-01' + INTERVAL y-1900 YEAR + INTERVAL m-1 MONTH + INTERVAL d-1
DAY FROM ...
Not sure that this any more efficient than using CONCAT and STR_TO_DATE.
不确定这是否比使用 CONCAT 和 STR_TO_DATE 更有效。
回答by Nikesh K
i hope this wil work out..
我希望这会解决..
str_to_date( concat( year( curdate( ) ) , '-', month( a.dob ) , '-', day( a.dob ) ) , '%Y-%m-%d' )
Where a.dob is my column's name which has DATE
其中 a.dob 是我的专栏名称,其中包含 DATE