MySQL 在 SELECT * 查询中格式化日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9087363/
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
Format date in SELECT * query
提问by PGrau
Is it possible to format a date field in a SELECT * query? I want to get all of the columns from my table but need a different format from the date.
是否可以在 SELECT * 查询中格式化日期字段?我想从我的表中获取所有列,但需要与日期不同的格式。
Something like:
就像是:
SELECT *, FORMATDATE(date,'%m-%d-%y') AS date FROM my_table;
or do I have to process it in php after the query?
还是我必须在查询后在 php 中处理它?
回答by unutbu
回答by Average Joe
select DATE_FORMAT(your_date_field_name,'%m-%d-%y') AS whatever FROM your_table_name_here;
the result is something like this
结果是这样的
09-22-11
09-22-11
as opposed to this
与此相反
2011-02-02 15:42:51
2011-02-02 15:42:51
回答by yohanes
you can put name of column in back of date_format. like this :
您可以将列名放在 date_format 的后面。像这样 :
$rs = mysql_query("select blablabla,DATE_FORMAT(name_column, '%d-%b-%y | %H:%i')name_column,DATE_FORMAT(name_column2, '%d-%b-%y | %H:%i')as name_column2 from db ORDER BY id DESC limit $offset,$rows");
or you can put "as" yes, you must select one by one. its work !!!!
或者你可以把“作为”是的,你必须一一选择。这是工作 !!!!
回答by Nanne
I don't know what FORMATDATE
is, but you can use DATE-FORMAT
我不知道是什么FORMATDATE
,但你可以使用DATE-FORMAT
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
回答by sly
If you are writing application containing different timezones and formats better practice will be using UNIX timestamp stored in int(8) field, otherwise you can format date fields in queries.
如果您正在编写包含不同时区和格式的应用程序,更好的做法是使用存储在 int(8) 字段中的 UNIX 时间戳,否则您可以在查询中格式化日期字段。