Trying to find duplicate users by email, who have an item count less than 0. I’ve got the duplicate users working (though it returns the full list of users sorted, instead of a subset):
select users.id, email, count(email) as count
from users
group by users.email
order by count desc
I’m trying to join on Items where count(items.id) < 1, but that doesn’t seem to work.
select users.id, email, count(email) as count
from users
join items on items.user_id = users.id
having count(items.id) < 1
group by users.email
order by count desc
I also tried an IN query, but can’t seem to get the syntax right.
Easy way to do this? Thanks!
UPDATE:
This query did the trick:
select DISTINCT(u1.id), u1.email
from users u1
inner join users u2
on 1=1
and u1.email = u2.email
and u1.id != u2.id
where not exists (select * from items i where i.user_id = u1.id)
order by u1.id
Duplicated users:
For second one, try this:
Another version of second:
Make sure you have index on user_id in items table.