Table have the following data Each same ID has the same datetime.
ID | sendtime | gcm | type
1, 2010-07-14 12:12:12, 1111111, a
1, 2010-07-14 12:12:12, 2222222, b
1, 2010-07-14 12:12:12, 3333333, c
2, 2010-07-10 11:11:11, 4444444, d
2, 2010-07-10 11:11:11, 5555555, c
3, 2010-07-15 13:13:13, 6666666, b
4, 2010-07-14 14:14:14, 7777777, a
I want to get values following
ID | sendtime | gcm | type
2, 2010-07-10 11:11:11, 4444444, d
2, 2010-07-10 11:11:11, 5555555, c
Result means to get all rows having same ID and COUNT(*) > 1 and MIN(datetime).
I tried the following query.
SELECT *
FROM USER a INNER JOIN
(SELECT ID, MIN(sendtime)
FROM USER
GROUP BY ID
HAVING COUNT(*) > 1
) b
ON a.ID = b.ID;
But result is
1, 2010-07-14 12:12:12, 1111111, a
1, 2010-07-14 12:12:12, 2222222, b
1, 2010-07-14 12:12:12, 3333333, c
2, 2010-07-10 11:11:11, 4444444, d
2, 2010-07-10 11:11:11, 5555555, c
How to get right values?
Here is one way to express your logic:
select u.*
from user u
where u.sendtime = (select min(u2.sendtime) from user u2 where u2.id = u.id
) and
(select count(*)
from user u2
where u2.id = u.id and u2.sendtime = u.sendtime
) >= 2;
I solved this work.
SELECT a.* FROM USER a INNER JOIN (SELECT ID FROM USER GROUP BY ID HAVING COUNT(*) > 1 ORDER BY SENDTIME ASC LIMIT 1 ) b ON a.ID = b.ID;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.