2016-09-15 61 views
1

組之前,我有以下的表稱爲uc_likesmysql命令由DAY(時間戳)

CREATE TABLE `uc_likes` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `type` int(255) NOT NULL, 
    `dwable` int(255) NOT NULL, 
    `actionby` int(255) NOT NULL, 
    `actionto` int(255) NOT NULL, 
    `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=257 ; 

INSERT INTO `uc_likes` (`id`, `type`, `dwable`, `actionby`, `actionto`, `time`) VALUES 
(38, 1, 309, 4, 1, '2016-09-12 13:04:01'), 
(41, 1, 372, 4, 1, '2016-09-15 13:04:07'), 
(42, 1, 373, 4, 1, '2016-09-10 13:04:08'), 
(55, 1, 416, 4, 1, '2016-09-15 13:04:09'), 
(253, 1, 307, 5, 1, '2016-09-15 13:04:12'), 
(256, 1, 372, 5, 1, '2016-09-15 13:04:13'); 

SELECT * FROM uc_likes; 
+-----+------+--------+----------+----------+---------------------+ 
| id | type | dwable | actionby | actionto | time    | 
+-----+------+--------+----------+----------+---------------------+ 
| 38 | 1 | 309 |  4 |  1 | 2016-09-12 13:04:01 | 
| 41 | 1 | 372 |  4 |  1 | 2016-09-15 13:04:07 | 
| 42 | 1 | 373 |  4 |  1 | 2016-09-10 13:04:08 | 
| 55 | 1 | 416 |  4 |  1 | 2016-09-15 13:04:09 | 
| 253 | 1 | 307 |  5 |  1 | 2016-09-15 13:04:12 | 
| 256 | 1 | 372 |  5 |  1 | 2016-09-15 13:04:13 | 
+-----+------+--------+----------+----------+---------------------+ 

http://sqlfiddle.com/#!9/62794a

我試圖讓

+-----+------+--------+----------+----------+---------------------+ 
| id | type | dwable | actionby | actionto | time    | 
+-----+------+--------+----------+----------+---------------------+ 
| 256 | 1 | 372 |  5 |  1 | 2016-09-15 13:04:13 | 
| 253 | 1 | 307 |  5 |  1 | 2016-09-15 13:04:12 | 
| 55 | 1 | 416 |  4 |  1 | 2016-09-15 13:04:09 | 
| 38 | 1 | 309 |  4 |  1 | 2016-09-12 13:04:01 | 
| 42 | 1 | 373 |  4 |  1 | 2016-09-10 13:04:08 | 
+-----+------+--------+----------+----------+---------------------+ 

此省略了ID 41因爲它在同一天期間(15日)已經具有相同的dwable值的較晚的條目。同樣假設,如果編號爲41的行在14日有時間戳,則應將其包括在內。

至於其他的答案都建議我使用子查詢如下分組之前訂購的嘗試:

SELECT * FROM 
(SELECT * FROM `uc_likes` WHERE `actionto` = 1 ORDER BY time DESC) 
AS t GROUP BY DAY(time), dwable order by time DESC; 

然而,這仍然拿起ID 41了ID 256。看看下面的結果:

+-----+------+--------+----------+----------+---------------------+ 
| id | type | dwable | actionby | actionto | time    | 
+-----+------+--------+----------+----------+---------------------+ 
| 256 | 1 | 372 |  5 |  1 | 2016-09-15 13:04:13 | 
| 253 | 1 | 307 |  5 |  1 | 2016-09-15 13:04:12 | 
| 55 | 1 | 416 |  4 |  1 | 2016-09-15 13:04:09 | 
| 38 | 1 | 309 |  4 |  1 | 2016-09-12 13:04:01 | 
| 42 | 1 | 373 |  4 |  1 | 2016-09-10 13:04:08 | 
+-----+------+--------+----------+----------+---------------------+ 
+0

請它是您創建的實例的sqlfiddle(sqlfiddle.com)更好。所以,我們可以重新創建你的問題。 – geeksal

+0

http://sqlfiddle.com/#!9/62794a @Strawberry – Ben

+0

@geeksal完成。我以前從未使用過它,所以我希望我做對了。 – Ben

回答

1

例如爲:

SELECT x.* 
    FROM uc_likes x 
    JOIN 
    (SELECT dwable 
      , DATE(time) dt 
      , MAX(time) time 
     FROM uc_likes 
     GROUP 
      BY dwable 
      , dt 
    ) y 
    ON y.dwable = x.dwable 
    AND y.dt = DATE(x.time) 
    AND y.time = x.time; 
+-----+------+--------+----------+----------+---------------------+ 
| id | type | dwable | actionby | actionto | time    | 
+-----+------+--------+----------+----------+---------------------+ 
| 38 | 1 | 309 |  4 |  1 | 2016-09-12 13:04:01 | 
| 42 | 1 | 373 |  4 |  1 | 2016-09-10 13:04:08 | 
| 55 | 1 | 416 |  4 |  1 | 2016-09-15 13:04:09 | 
| 253 | 1 | 307 |  5 |  1 | 2016-09-15 13:04:12 | 
| 256 | 1 | 372 |  5 |  1 | 2016-09-15 13:04:13 | 
+-----+------+--------+----------+----------+---------------------+ 
+0

效果很好。感謝您的編輯/格式問題提示。 – Ben