有没有办法在不使用自连接的情况下解决这样的问题?使用 min() 函数的某种方式?
我想为每组列 c1 和 c2 获取第一个水果条目。(假设日期不能相同)
DROP TABLE IF EXISTS test;
CREATE TABLE test
(
c1 varchar(25),
c2 varchar(25),
fruit varchar(25),
currentTime Datetime
);
INSERT INTO test VALUES
('a','b','pineapple','2013-01-28 20:50:00'),
('a','b','papaya','2013-01-28 20:49:00'),
('a','b','pear','2013-01-28 20:51:00'),
('a','c','peach','2013-01-28 18:12:00'),
('a','c','plum','2013-01-28 20:40:00'),
('a','c','pluot','2013-01-28 16:50:00');
这是我当前的查询:
SELECT t2.*
FROM (SELECT c1,
c2,
MIN(currentTime) AS ct
FROM test
GROUP BY c1, c2) as t1
JOIN test t2
ON t1.c1 = t2.c1 AND
t1.c2 = t2.c2 AND
t2.currentTime = t1.ct
这会为每对产生最早的条目c1/c2
,但是有没有办法使用min()
和避免自连接?