MySQL之前按DAY(时间戳)排序

时间:2016-09-15 16:46:35

标签: mysql

我有下表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 | +-----+------+--------+----------+----------+---------------------+

+-----+------+--------+----------+----------+---------------------+
| 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 |
+-----+------+--------+----------+----------+---------------------+

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

我想要

41

这省略了id 41行,因为它已经在同一天(15日)内具有相同dwable值的后一个条目。同样假设,如果id SELECT * FROM (SELECT * FROM `uc_likes` WHERE `actionto` = 1 ORDER BY time DESC) AS t GROUP BY DAY(time), dwable order by time DESC; 行在14日有时间戳,例如它应该被包括在内。

正如其他答案所示,我在分组之前尝试使用子查询进行排序,如下所示:

41

但是,仍会在ID 256上选择ID +-----+------+--------+----------+----------+---------------------+ | 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 | +-----+------+--------+----------+----------+---------------------+ 。见下面的结果:

Error: iconUrl not set in Icon options (see the docs).


...tIconUrl(t);if(!i){if("icon"===t)throw new Error("iconUrl not set in Icon option...

1 个答案:

答案 0 :(得分:1)

E.g:

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 |
+-----+------+--------+----------+----------+---------------------+