2012-11-20 54 views
3

,比如我有如下表:MySQL查詢每個月選擇項目組

items 
----- 
item_id (usigned-int, not-null, auto-increment, primary) 
created_at (datetime, not-null) 
... 

,我有以下數據集:

(1, 01/01/2012) 
(2, 03/01/2012) 
(3, 07/01/2012) 
(4, 12/01/2012) 
(5, 28/01/2012) 
(6, 02/02/2012) 
(7, 06/02/2012) 
(8, 13/02/2012) 
(9, 27/02/2012) 

我希望得到以下結果:

(3, 07/01/2012) 
(4, 12/01/2012) 
(5, 28/01/2012) 
(8, 13/02/2012) 
(9, 27/02/2012) 

即,每個月的每第一個(n = 2)記錄被跳過。

我怎樣才能實現這與MySQL?

回答

1

它基於分區中的項目的排名上的選擇:

create table items (
    item_id int, 
    created_at datetime 
); 


insert into items 
select 1, '2012-01-01' union 
select 2, '2012-01-03' union 
select 3, '2012-01-07' union 
select 4, '2012-01-12' union 
select 5, '2012-01-28' union 
select 6, '2012-02-02' union 
select 7, '2012-02-06' union 
select 8, '2012-02-13' union 
select 9, '2012-02-28' 


select i.* 
from items i 
join (
    select t.item_id, count(item_id1) as row_no 
    from (
    select t.item_id, t1.item_id as item_id1 
    from items t 
    join items t1 on t.item_id >= t1.item_id and 
     DATE_FORMAT(t.created_at, '%Y%m') = DATE_FORMAT(t1.created_at, '%Y%m') 
) t 
    group by t.item_id 
) g on i.item_id = g.item_id 
where g.row_no > 2 

Live example on SQL Fiddle

+0

我喜歡你如何合併年份和月份檢查一起。好主意和良好的介紹。謝謝! –

1

嘗試此查詢 -

SELECT id, created_at FROM (
    SELECT t1.*, COUNT(*) r FROM items t1 
    LEFT JOIN items t2 
     ON MONTH(t2.created_at) = MONTH(t1.created_at) AND t2.id <= t1.id 
    GROUP BY 
    MONTH(t1.created_at), t1.id 
) t 
WHERE r > 2; 

您還需要在今年進行過濾,或只加條件 -

SELECT id, created_at FROM (
    SELECT t1.*, COUNT(*) r FROM items t1 
    LEFT JOIN items t2 
     ON YEAR(t2.created_at) = YEAR(t1.created_at) AND 
     MONTH(t2.created_at) = MONTH(t1.created_at) AND 
     t2.id <= t1.id 
    GROUP BY 
    YEAR(t1.created_at), MONTH(t1.created_at), t1.id 
) t 
WHERE r > 2; 
+0

這是個好主意,謝謝。投票,但我不得不選擇另一個答案作爲一個正確的。 –

0
SELECT item_id, created_at, 
    CASE WHEN @month IN (RIGHT(created_at, 7), 0) 
     THEN @rownum := @rownum + 1 
     ELSE @rownum := 1 END AS rownum, 
    @month = RIGHT(created_at, 7) AS dummy 
FROM ( SELECT item_id, created_at 
     FROM items 
     ORDER BY created_at DESC) AS h, 
    ( SELECT @rownum := 0, @month := 0) AS vars 
HAVING rownum > 2 
+0

我不確定它是否正常工作。作爲結果,我得到初始數據集,按相反順序排序。 –