如何显示当前年份中指定季度的最后一个月的记录


How to display the record from the last month of the specified quarter within the current year?

我有一个名为nca_totals的表。

+----------+-----------+------------+|total_id |nca_total |nca_date |+----------+-----------+------------+|      17 |8000 |2015-01-01 ||      18 |2000年 |2015-04-01 ||      19 |6000 |2015-07-02 ||      20 |2000年 |2015-01-22 ||      21 |6000 |2015-03-02 ||      22 |5000 |2015-06-01 |+----------+-----------+------------+

我想显示当前年份和该季度最后一个月属于第一季度的所有行。例如,我想从第一季度返回三月(2015-03-01)。在下一季度第二季度,必须显示6月份(2015-06-01)。

到目前为止,我已经通过此查询来显示属于第一季度的所有记录,但它显示了该季度的所有月份,其中这不是我想要的,只是该季度的最后一个月

SELECT * FROM nca_totals WHERE QUARTER(nca_date) = 1 AND year(nca_date) = year('2015-01-01');

但是此查询有问题,因为它显示以下内容:

+----------+-----------+------------+|total_id |nca_total |nca_date |+----------+-----------+------------+|      17 |8000 |2015-01-01 ||      20 |2000年 |2015-01-22 ||      21 |6000 |2015-03-02 ||      22 |5000 |2015-06-01 |+----------+-----------+------------+

以下是我想在查询中输出的内容:

+----------+-----------+------------+|total_id |nca_total |nca_date |+----------+-----------+------------+|      21 |6000 |2015-03-02 |+----------+-----------+------------+

如何处理从第一季度获得最后一个月?谁能帮忙?谢谢

季度的最后一个月保持不变将始终是 (3,6,9,12),因此您可以这样做:

SELECT * FROM nca_totals WHERE MONTH(nca_date) in (3,6,9,12) AND year(nca_date) = year('2015-01-01');