superuseroi
superuseroi

Reputation: 1348

Finding duplicates in MYSQL table where data is in multiple tables (multiple conditions needed)

My knowledge of MYSQL is rather basic, I would appreciate help with the following:

subrecords schema

id | record | element | title | name | type | value

The above table stores data submitted via a web form.

records schema

id | submitted | form | title | name

I need to find all users (by email) that submitted the same form more than once, here are some further clarifications:

So far thanks to another user I have this:

SELECT value     as email
      ,record
      ,COUNT(*)  as form_count
  FROM subrecords
 WHERE title = 'email'
   AND record IN (SELECT id
                    FROM records
                   WHERE name = 'form_name'
                 )
 GROUP BY value
         ,record
HAVING COUNT(*) > 1

It returns an empty value, but I am unable to narrow down how to improve this to make it work. Thank you

Upvotes: 1

Views: 4085

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270713

I think the problem is that you are grouping by record. A given record only has one form.

However, your query would be improved by switching from in to a join. Also, I'm not clear if you have a specific form name in mind. If so, then add and r.name = 'form_name' to the on clause:

SELECT r.form, s.value as email, COUNT(distinct s.record)  as form_count
FROM subrecords s join
     records r
     on s.record = r.id
WHERE s.title = 'email'
GROUP BY s.value, r.form
HAVING form_count > 1;

Upvotes: 1

Related Questions