2012-09-17 42 views
0

這裏是我的數據集團類似物品

 
    id  actor_id created_at    updated_at 

    729 80   2012-09-10 17:05:59 2012-09-10 17:05:59 
    731 80   2012-09-10 17:04:02 2012-09-10 17:04:02 
    725 139  2012-09-06 13:59:08 2012-09-06 13:59:08 
    724 76   2012-09-06 11:31:30 2012-09-06 11:31:30 
    723 29   2012-09-06 09:40:22 2012-09-06 09:40:22 
    719 29   2012-09-06 09:24:02 2012-09-06 09:24:02 
    811 80   2012-09-02 17:05:59 2012-09-10 17:05:59 
    812 80   2012-09-01 17:04:02 2012-09-10 17:04:02 

表結構這是

SELECT `te`.`id`, te.actor_id, te.created_at, te.created_at 
FROM `timeline_events` AS te 
ORDER BY 
    te.created_at DESC 
    LIMIT 10 

結果我被actor_id和created_at需要組

以下是我需要結束

 
    id actor_id  created_at   updated_at    count 

    729 80  2012-09-10 17:05:59 2012-09-10 17:05:59  2 
    725 139  2012-09-06 13:59:08 2012-09-06 13:59:08  1 
    724 76  2012-09-06 11:31:30 2012-09-06 11:31:30  1 
    723 29  2012-09-06 09:40:22 2012-09-06 09:40:22  2 
    812 80  2012-09-10 17:04:02 2012-09-10 17:04:02  2 

有人可以指導我如何做到這一點?

提前

UPD非常感謝簡化我再舉一個例子

所以說,我有下一行

 
1            1 (count: 2) 
1 
3            3 (count: 1) 
4 
4  => after magic function it should be 4 (count: 2) 
1 
1            1 (count: 3) 
1 
6            6 (count: 2) 
6 
4            4 (count 3) 
4 
4 

所以應該按組劃分。

UPD 2 我需要這個查詢來渲染時間線。現在它顯示所有信息用戶做了什麼,但我需要分組。

之前

  • USER1上傳圖片
  • user1的改變信息
  • user2的更新生物
  • 用戶3上傳照片
  • 用戶3更新生物
  • USER1更新生物

  • USER1 uploadd照片並改變infomarion
  • user2的更新生物
  • 用戶3上傳的照片和更新的生物
  • USER1更新生物
+0

很好的問題,這在我的經驗,很多人留下了。這裏有一個類似的查詢鏈接: http://stackoverflow.com/questions/514943/php-mysql-order-by-two-columns –

+0

什麼聚合函數你想使用字段'ID'?例如,爲什麼你想要729 80而不是'731 80'? –

+0

@MahmoudGamal id這個查詢並不重要。此查詢在時間軸 – timsly

回答

1

我自己找解答

下面是該查詢

 
SET @i = 0; 
SELECT 
    COUNT(`wrapper`.`id`) AS 'count', 
    GROUP_CONCAT(`wrapper`.`type` SEPARATOR ',') as 'types' 
FROM (
    SELECT 
     @prev := (
      SELECT prev_te.actor_id 
      FROM `timeline_events` AS prev_te 
      WHERE prev_te.created_at > te.created_at 
      ORDER BY prev_te.created_at ASC 
      LIMIT 1 
     ) AS 'prev_actor_id', 
     IF(
      @prev = te.`actor_id` OR @prev IS NULL, 
      @i, 
      @i := @i + 1 
     ) AS 'actor_id_group', 
     `te`.* 
    FROM `timeline_events` AS te 
    ORDER BY 
     te.created_at DESC, 
     te.id DESC 
) AS `wrapper` 
GROUP BY `wrapper`.`actor_id_group` 
LIMIT 10 

這裏是the proof link ;-)

This website真的幫了我

我使用的包裝進行分組的目的,因爲MySQL並沒有GROUP BY變量,如果有人知道更好的解決方案,請讓我知道

0

添加GROUP BY actor_id之前order by條款。

最新編輯: select distinct actor_id, count(actor_id), created_at from actors group by actor_id order by created_at desc;

+0

但是,與字段「id」和其他不在group by子句中的字段一起使用的聚合函數是什麼? –

+0

@sweettea如果我添加GROUP BY,它將按所有ID進行分組,但我需要按組進行分組(我已添加anohter示例) – timsly

+0

嘗試ORDER BY te.created_at DESC,te.actor_id。這將首先由created_at命令,然後由actor_id命令。 –

0

選擇teid,te.actor_id,te.created_at,te.updated_at, COUNT(*)FROM timeline_events AS te GROUP BY te.actor_id, te.created_at ORDER BY te。created_at DESC LIMIT 10

+0

這是不正確的,因爲te.id'不在group by子句中,也不在聚合函數中 –

+0

是的,這是不是我所需要的,因爲我不需要所有ids組在一行下 – timsly

2

我認爲這可能是你正在嘗試做的事:

select t1.id, 
    t1.actor_id, 
    max(created_at) created_at, 
    updated_at, 
    t2.total 
from yourtable t1 
left join 
(
    select count(*) total, date(created_at) dt, actor_id 
    from yourtable 
    group by actor_id, date(created_at) 
) t2 
    on t1.actor_id = t2.actor_id 
    and date(t1.created_at) = t2.dt 
group by t1.actor_id, date(t1.created_at) 
order by t1.created_at desc 

看到SQL Fiddle with demo

我被actor_id分組,並使用DATE()功能

日期
+0

謝謝,非常相似。但爲什麼它沒有分組最後兩列(其中actor_id是80) – timsly

+0

也許這是我的錯,因爲我沒有exvellain正確 所以它不應該使用created_at組,它應該只按這個字段排序,但group by actor_id – timsly

+0

@ neimo0o我在子查詢和外部查詢中都用'group_ by'created_at'字段。我只是將'DATE()'函數應用於它,以僅返回日期部分,並刪除由created_at字段組合的時間 – Taryn