Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a MySQL table reqs with (this is simplified from actual):

| Field                 | Type                  | Null | Key | Default | Extra |
| time                  | datetime              | YES  |     | NULL    |       |
| user                  | varbinary(40)         | YES  |     | NULL    |       |
| foo                   | mediumint(8) unsigned | YES  |     | NULL    |       |
| bar                   | varbinary(20)         | YES  |     | NULL    |       |
| other fields          | ...

I want to find duplicate user-foo-bar combinations near one another in time — say, within 60 seconds of one another. So I want something like

select [whatever] from reqs inner join (
select user,foo,bar, count(*) as count from reqs
group by user,foo,bar having count>1
) as dups
where dups.user=reqs.user
and =
and [something indicating the times are near each other];

— or at least that's the idea (in other words, I'm not sure whether the above is the way to structure the query, but the above is representative of the result I want from the query).

How would I do this?

Note that reqs is a huge table, so minimizing joins would be ideal.

share|improve this question

The problem is, when you GROUP on user,foo,bar you are not able to get a list of each of those times to compare.

Something like this might work, but no idea on the performance on a large table. You might want to only do this on a subset (ie tuples just from the last 30 days)

SELECT A.*,B.time FROM reqs A
INNER JOIN (SELECT user,foo,bar,time FROM reqs) B 
 USING (user,foo,bar)
GROUP BY A.user,,,A.time;
share|improve this answer
There I go again, overthinking the problem. I think you got the silver bullet on this one !!! +1 !!! – RolandoMySQLDBA Sep 23 '11 at 2:03
+1; thank you! This looks good; I'll try it. – msh210 Sep 23 '11 at 14:42
When comparing where A.user=B.user and and (I actually have not three but five columns to check for equality), would it be cheaper to use where strcmp(concat(a.user,,,concat(b.user,, – msh210 Sep 23 '11 at 16:40
@msh210 hm, I would actually use USING clause (going to update the sql for it): – Derek Downey Sep 23 '11 at 17:09
@Rolando the problem is, you don't know if A.time is greater or less than B.time. If A.time is smaller, without ABS() you get negatives, and -12,000 < 60 – Derek Downey Sep 23 '11 at 17:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.