我有一张桌子(附有图像)。
我只想根据persistent_token否选择不同的条目。我想要这样的结果:
如何仅基于mysql中的一列选择不同的条目?
最佳答案
以下仅是示例,您需要将单独的日期和时间列作为单独的问题进行计算。
SQL Fiddle
MySQL 5.6模式设置:
CREATE TABLE Table1
(`date` date, `time` time, `permanent_token` varchar(8), `RID` int, `SID` int)
;
INSERT INTO Table1
(`date`, `time`, `permanent_token`, `RID`, `SID`)
VALUES
('2015-08-04 00:00:00', '12:40:41', 'HPC12334', 12, 34),
('2015-08-04 00:00:00', '15:15:15', 'HPC12334', 18, 37),
('2015-08-04 00:00:00', '08:09:10', 'ABX2334', 48, 47)
;
查询1:
select t.*
from table1 as t
inner join (
select permanent_token, `date` , MIN(`time`) as minTime
from table1
group by permanent_token, `date`
) as gby on t.permanent_token = gby.permanent_token
and t.`date` = gby.`date`
and t.`time` = gby.minTime
Results:
| date | time | permanent_token | RID | SID |
|--------------------------|---------------------------|-----------------|-----|-----|
| August, 04 2015 00:00:00 | January, 01 1970 12:40:41 | HPC12334 | 12 | 34 |
| August, 04 2015 00:00:00 | January, 01 1970 08:09:10 | ABX2334 | 48 | 47 |