2015-01-21 103 views
0

我有一個名爲nca_totals的表。在mysql中按季度顯示記錄

 
Table: nca_totals 
+----------+-----------+------------+--------------+ 
| total_id | nca_total | nca_date | account_type | 
+----------+-----------+------------+--------------+ 
|  13 | 10450  | 2015-01-21 | DBP-TRUST | 
|  14 | 5000  | 2015-02-05 | DBP-TRUST | 
|  15 | 7000  | 2015-04-02 | DBP-TRUST | 
|  16 | 4000  | 2015-05-02 | DBP-TRUST | 
+----------+-----------+------------+--------------+ 

現在我想按季度顯示所有數據。比方說,我想顯示誰屬於第一季度這樣的所有記錄:

 
+----------+-----------+------------+--------------+ 
| total_id | nca_total | nca_date | account_type | 
+----------+-----------+------------+--------------+ 
|  13 | 10450  | 2015-01-21 | DBP-TRUST | 
|  14 | 5000  | 2015-02-05 | DBP-TRUST | 
+----------+-----------+------------+--------------+ 

該日期屬於今年第一季度(一月,二月,三月)。我只有這個查詢選擇日期並返回季度數量:

SELECT QUARTER('2015-01-11');  /* returns 1 */ 

我如何可以結合該查詢顯示按季度所有記錄?誰能幫忙?謝謝。

回答

0
select * 
from nca_totals 
where QUARTER(nca_date) = 1 
+0

添加一些解釋你的答案吧。 – 2015-01-21 11:05:53

0
SELECT 
    CEIL(MONTH(`nca_date`)/3) AS `quarter` 
FROM `nca_totals`;