简体   繁体   English

如何根据最新日期选择记录

[英]how to I select records based on the latest date

I am trying to get the latest record only. 我想要获得最新的记录。 It works fine if i don't include "CustFile.optin" but once I add this field, it gives me all the records. 如果我不包含“CustFile.optin”,它可以正常工作但是一旦我添加了这个字段,它就会给我所有的记录。 Can you please help? 你能帮忙吗?

SELECT BEST.fname, BEST.lname, BEST.email, MAX(CustFile.status_date), CustFile.optin
FROM  BEST, CustFile
WHERE BEST.email = CustFile.email
GROUP BY BEST.fname, BEST.lname, BEST.email, CustFile.optin

Answer: 回答:

fname    lname      email    optin  date
a.fname  a.lname    aa@email    y   01/08/2015
b.fname  b.lname    bb@email    n   16/06/2016

Why not simply take the row WHERE MAX(CustFile.status_date) = CustFile.status_date ? 为什么不简单地将行WHERE MAX(CustFile.status_date) = CustFile.status_date

SELECT * 
FROM BEST, CustFile 
WHERE 
    BEST.email = CustFile.email AND 
    MAX(CustFile.status_date) = CustFile.status_date

Or order by date (desc) and take only the top most as @Paul-Spiegel says 或者按日期排序(desc),只采用@ Paul-Spiegel所说的最高级别

SELECT * 
FROM BEST, CustFile 
WHERE BEST.email = CustFile.email
ORDER BY status_date DESC 
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM