想要与特定日期匹配的记录应仅检查具有最新日期的记录

时间:2019-04-12 10:57:42

标签: mysql sql

有一个包含查询日志的表。该表可以包含一个查询的多个日志。 我想要与特定日期匹配的记录,并且只应检查具有最新日期的记录。

下面是我的代码。请帮助我

SELECT email_log.enquiry_id,email_log.email_log_date,enquiry.en_movetype,enquiry.en_unique_id, enquiry.en_movetype,enquiry.en_fname,enquiry.en_date,email_log.email_log_id 
from email_log 
JOIN enquiry ON email_log.enquiry_id = enquiry.enquiry_id 
WHERE email_log.email_log_date = DATE_FORMAT('2019-04-05 12:53','%Y-%m-%d %H:%i')
GROUP BY email_log.enquiry_id
ORDER By email_log.email_log_date DESC

现在我得到以下结果。enter image description here

它给我的日期仅与给定的日期匹配。我想查询已注册了最大电子邮件时间的查询。

在下图中,email_log_id 15和28是最新的日志。我希望查询检查这两条记录是否与特定日期匹配。如果匹配,则应返回这些记录。

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为使用Max或sql的First函数可以提供帮助

答案 1 :(得分:0)

查看您的日期,似乎您正在寻找 记录为2019-04-05

SELECT email_log.enquiry_id
    ,email_log.email_log_date
    ,enquiry.en_movetype
    ,enquiry.en_unique_id
    , enquiry.en_movetype
    ,enquiry.en_fname
    ,enquiry.en_date
    ,email_log.email_log_id 
from email_log 
JOIN enquiry ON email_log.enquiry_id = enquiry.enquiry_id 
WHERE email_log.email_log_date = date(str_to_date( '2019-04-05 12:53:00','%Y-%m-%d %T'))

这是否意味着您想要最后一个日期的日志

SELECT email_log.enquiry_id
    ,email_log.email_log_date
    ,enquiry.en_movetype
    ,enquiry.en_unique_id
    , enquiry.en_movetype
    ,enquiry.en_fname
    ,enquiry.en_date
    ,email_log.email_log_id 
from email_log 
JOIN enquiry ON email_log.enquiry_id = enquiry.enquiry_id 
INNER JOIN  (
  SELECT max( date( email_log.email_log_date)  as max_date 
  from email_log 
  JOIN enquiry ON email_log.enquiry_id = enquiry.enquiry_id 

) t On t.max_date  = date( email_log.email_log_date)