2

我需要我的查询来选择从特定过去日期到现在的 unix 时间戳?例如,我需要选择“2013-01-01”到当前日期。我能够在特定的年份做到这一点。任何帮助,将不胜感激。

SELECT mdl_user_info_data.data, mdl_user.firstname, mdl_user.lastname, mdl_user.id   AS  userid, SUM( mdl_quiz.fcpd ) AS cpdtotal
FROM mdl_grade_grades
INNER JOIN mdl_user ON mdl_grade_grades.userid = mdl_user.id
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.id
INNER JOIN mdl_quiz ON mdl_grade_items.itemname = mdl_quiz.name
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid
WHERE mdl_user_info_data.fieldid =1
AND mdl_grade_items.itemname IS NOT NULL
AND YEAR( FROM_UNIXTIME( mdl_grade_grades.timemodified ) ) =2013
GROUP BY mdl_user.id
4

5 回答 5

1

要检查过去给定日期时间和当前日期时间之间的时间戳值,如下所示:

AND mdl_grade_grades.timemodified >= '2013-07-25 15:30'
AND mdl_grade_grades.timemodified <  NOW()
于 2013-09-16T06:06:49.813 回答
0

只需替换此行:

AND YEAR(FROM_UNIXTIME(mdl_grade_grades.timemodified)) = 2013

至:

AND FROM_UNIXTIME(mdl_grade_grades.timemodified)
   BETWEEN '2012-01-01 12:00'
   AND now()

您似乎在timemodified列中存储了 unix 时间戳。您应该考虑将其更改为timestamptype - 这将允许利用该字段上的索引。现在,您的查询(以及此查询)很慢,因为它必须计算FROM_UNIXTIME()该表中的每一行,然后才比较年份或日期。

更新

如果您不想更改类型,使用它应该会使您的查询工作得更快(假设timemodified列上有索引):

AND mdl_grade_grades.timemodified
   BETWEEN unix_timestamp('2012-01-01 12:00')
   AND unix_timestamp(now())
于 2013-09-16T07:14:43.190 回答
0

您可以创建一个存储过程并将日期作为参数传递给它。在这里,我也通过添加结束日期使其更加灵活。

CREATE PROCEDURE `NewProc`(IN StartDate date,IN EndDate date)
BEGIN

SELECT mdl_user_info_data.data, mdl_user.firstname, mdl_user.lastname, mdl_user.id   AS  userid, SUM( mdl_quiz.fcpd ) AS cpdtotal
FROM mdl_grade_grades
INNER JOIN mdl_user ON mdl_grade_grades.userid = mdl_user.id
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.id
INNER JOIN mdl_quiz ON mdl_grade_items.itemname = mdl_quiz.name
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid
WHERE mdl_user_info_data.fieldid =1
AND mdl_grade_items.itemname IS NOT NULL
AND mdl_grade_grades.timemodified between StartDate and EndDate 
GROUP BY mdl_user.id

END;
于 2013-09-16T06:46:40.543 回答
0

试试下面的查询

SELECT mdl_user_info_data.data, mdl_user.firstname, mdl_user.lastname, mdl_user.id   AS  userid, SUM( mdl_quiz.fcpd ) AS cpdtotal
FROM mdl_grade_grades
INNER JOIN mdl_user ON mdl_grade_grades.userid = mdl_user.id
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.id
INNER JOIN mdl_quiz ON mdl_grade_items.itemname = mdl_quiz.name
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid
WHERE mdl_user_info_data.fieldid =1
AND mdl_grade_items.itemname IS NOT NULL
AND mdl_grade_grades.timemodified <  date('d-m-Y')
GROUP BY mdl_user.id
于 2013-09-16T06:04:02.287 回答
0
AND DATE(FROM_UNIXTIME(mdl_grade_grades.timemodified)) between '2013-01-01' and  
DATE(NOW()); 
于 2013-09-16T06:12:03.180 回答