简体   繁体   English

mysql在3个月内未检查任何记录,组限制为1,显示最后一笔交易

[英]mysql check no records within 3 months with group limit by 1 show the last transaction

i want to have a mysql query that generates data without records 3 months period. 我想拥有一个mysql查询,该查询生成的数据没有记录3个月。 Display the latest transaction group by name limit by 1. 按名称限制显示最新的交易组1。

**Sample data (March to May)**

Name    Date of Transaction 
Robert     2014-03-03
Angel      2014-02-25
Robert     2014-06-03
Daniel     2014-03-11
Angel      2014-05-31
Christine  2014-01-31
Henry      2014-05-05
Henry      2014-06-01
Nicole     2014-03-25

**It should display:**

Name    Date of Transaction 
Daniel     2014-03-11
Nicole     2014-03-25
Angel      2014-05-31
Henry      2014-06-01
Robert     2014-06-03

Is this possible? 这可能吗? Here is my query but not working 这是我的查询但不起作用

SELECT *
FROM <tablename>
WHERE date <= CURDATE() - INTERVAL 3 MONTH
GROUP BY name
ORDER BY date
DESC

EDIT 编辑

How about without the no records within 3 months? 3个月内没有记录怎么办?

Name    Date of Transaction 
Christine  2014-01-31

When you use group by you can only display columns in your group by clause unless using an aggregate function. 使用group by时,除非使用聚合函数,否则只能在group by子句中显示列。 Does this help? 这有帮助吗?

SELECT name, max(dtTransaction)
FROM <tablename>
WHERE date <= CURDATE() - INTERVAL 3 MONTH
GROUP BY name

Use max to get the latest date. 使用max获取最新日期。

You can try something like this:- 您可以尝试这样的事情:

SELECT Name, MAX(Date of Transaction)
FROM <tablename>
WHERE date <= CURDATE() - INTERVAL 3 MONTH
GROUP BY Name
ORDER BY date; 

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

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