SIm是编码的新手,但我有一个MYSQL查询,它返回所需的信息,除了它将我的日期字段(d.timestart和d.timefinish)作为unixtimestamps返回。 我知道我需要使用From Unixtime(),但我不知道如何插入它。
这是我的代码可以正常工作:)
SELECT g.quiz AS'Quiz Number',
g.userid AS'User Number',
g.grade AS'Grade Percentage Achieved',
u.firstname AS' First Name',
u.lastname AS'Last Name',
c.name AS'Assessment Name',
d.attempt AS'Number of Attempts',
d.state AS'Assessment Progress',
d.timestart AS'Assessment Started',
d.timefinish AS'Assessment Finished'
FROM mdl_quiz_grades g
JOIN mdl_user u
ON u.id = g.userid
JOIN mdl_quiz c
ON c.id = g.quiz
JOIN mdl_quiz_attempts d
ON d.id = g.userid
谢谢Gary
答案 0 :(得分:2)
您可以使用FROM_UNIXTIME
:
SELECT g.quiz AS'Quiz Number',
g.userid AS'User Number',
g.grade AS'Grade Percentage Achieved',
u.firstname AS' First Name',
u.lastname AS'Last Name',
c.name AS'Assessment Name',
d.attempt AS'Number of Attempts',
d.state AS'Assessment Progress',
From_unixtime(d.timestart) AS'Assessment Started',
From_unixtime(d.timefinish) AS'Assessment Finished'
FROM mdl_quiz_grades g
JOIN mdl_user u
ON u.id = g.userid
JOIN mdl_quiz c
ON c.id = g.quiz
JOIN mdl_quiz_attempts d
ON d.id = g.userid
如果您想格式化该日期,可以使用DATE_FORMAT
和说明符从the documentation格式化。
DATE_FORMAT(FROM_UNIXTIME(d.timestart), '%b %d %Y %h:%i %p') AS ...
答案 1 :(得分:1)
用于格式化unix时间戳,您可以尝试
SELECT FROM_UNIXTIME(unix_timestamp, '%m/%d/%Y') FROM myTable;
其中第二部分是格式字符串。
要格式化任何类型的日期和时间,您可以尝试DATE_FORMAT
功能,例如
SELECT DATE_FORMAT(date, '%m/%d/%Y') FROM myTable;
无论如何看看
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
和
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
答案 2 :(得分:0)
MySQL documentation中有一些例子。
SELECT g.quiz AS'Quiz Number',
g.userid AS'User Number',
g.grade AS'Grade Percentage Achieved',
u.firstname AS' First Name',
u.lastname AS'Last Name',
c.name AS'Assessment Name',
d.attempt AS'Number of Attempts',
d.state AS'Assessment Progress',
FROM_UNIXTIME(d.timestart) AS'Assessment Started',
FROM_UNIXTIME(d.timefinish) AS'Assessment Finished'
FROM mdl_quiz_grades g
JOIN mdl_user u
ON u.id = g.userid
JOIN mdl_quiz c
ON c.id = g.quiz
JOIN mdl_quiz_attempts d
ON d.id = g.userid