答案 0 :(得分:0)
用于过滤的相关子查询可能是最简单的解决方案:
select t.*
from t
where t.timestamp = (select min(t2.timestamp)
from t t2
where t2.id = t.id
);
如果想要最早的总体记录,可能需要考虑日期:
select t.*
from t
where (t.date, t.time) in (select t2.date, t2.time
from t t2
where t2.id = t.id
order by t2.date desc, t2.time desc
);
或者如果您想在每个日期最早记录:
select t.*
from t
where t.timestamp = (select min(t2.timestamp)
from t t2
where t2.id = t.id and
t2.date = t.date
);
答案 1 :(得分:0)
对于MySQL 8.0,窗口函数通常是最有效的处理方式:
SELECT col1, col2, col3, col4, col5
FROM (
SELECT t.*, ROW_NUMBER() OVER(PARTITION BY col2 ORDER BY col4) rn
FROM mytable t
) x WHERE rn = 1
在早期版本中,我将对关联子查询使用NOT EXISTS
条件:
SELECT *
FROM mytable t
WHERE NOT EXISTS (
SELECT 1 FROM mytable t1 WHERE t1.col2 = t.col2 AND t1.col4 < t.col4
)
| col1 | col2 | col3 | col4 | col5 |
| ---- | ---- | ---------- | -------- | ---------- |
| 2 | AAA | Customer 1 | 07:00:00 | 2019-03-04 |
| 3 | BBB | Customer 2 | 15:00:00 | 2019-03-04 |
要使其高效执行,您需要在mytable(col2, col4)
上建立索引:
CREATE INDEX mytable_idx ON mytable(col2, col4);
如果您有多个具有相同col1
和col2
的记录,则可以使用列c1
添加其他条件以避免结果集中出现重复,据我所知该表的主键:
SELECT *
FROM mytable t
WHERE NOT EXISTS (
SELECT 1
FROM mytable t1
WHERE
t1.col2 = t.col2
AND (
t1.col4 < t.col4
OR (t1.col4 = t.col4 AND t1.col1 < t.col1)
)
)