1
組之前,我有以下的表稱爲uc_likes
mysql命令由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 |
+-----+------+--------+----------+----------+---------------------+
請它是您創建的實例的sqlfiddle(sqlfiddle.com)更好。所以,我們可以重新創建你的問題。 – geeksal
http://sqlfiddle.com/#!9/62794a @Strawberry – Ben
@geeksal完成。我以前從未使用過它,所以我希望我做對了。 – Ben