dbTalk Databases Forums  

select all users whose emails belong to two or more users

comp.databases.mysql comp.databases.mysql


Discuss select all users whose emails belong to two or more users in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
yawnmoth
 
Posts: n/a

Default select all users whose emails belong to two or more users - 08-09-2006 , 11:49 AM






Say I wanted to create a query to select all the users whose emails
belong to two or more users. How would I go about doing this?

Here's what I have, so far (and which hasn't worked):

SELECT * FROM users WHERE email IN (SELECT email FROM users GROUP BY
email HAVING count(*) > 1))

Any ideas?


Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: select all users whose emails belong to two or more users - 08-09-2006 , 12:27 PM







yawnmoth wrote:
Quote:
Say I wanted to create a query to select all the users whose emails
belong to two or more users. How would I go about doing this?

Here's what I have, so far (and which hasn't worked):

SELECT * FROM users WHERE email IN (SELECT email FROM users GROUP BY
email HAVING count(*) > 1))

Any ideas?
untested

SELECT t1.id, t1.username
FROM (

SELECT DISTINCT email, count( email )
FROM users
GROUP BY email
HAVING count( email ) >1
)t2
LEFT JOIN users t1 ON t1.email = t2.email



Reply With Quote
  #3  
Old   
Bill Karwin
 
Posts: n/a

Default Re: select all users whose emails belong to two or more users - 08-09-2006 , 12:50 PM



yawnmoth wrote:
Quote:
Say I wanted to create a query to select all the users whose emails
belong to two or more users. How would I go about doing this?
Here's one possible solution:

SELECT DISTINCT u1.*
FROM users AS u1 JOIN users AS u2
ON u1.email = u2.email AND u1.userid <> u2.userid;

Assuming userid is a primary key, or otherwise unique column.

Regards,
Bill K.


Reply With Quote
  #4  
Old   
yawnmoth
 
Posts: n/a

Default Re: select all users whose emails belong to two or more users - 08-09-2006 , 12:55 PM




strawberry wrote:
Quote:
yawnmoth wrote:
Say I wanted to create a query to select all the users whose emails
belong to two or more users. How would I go about doing this?

Here's what I have, so far (and which hasn't worked):

SELECT * FROM users WHERE email IN (SELECT email FROM users GROUP BY
email HAVING count(*) > 1))

Any ideas?

untested

SELECT t1.id, t1.username
FROM (

SELECT DISTINCT email, count( email )
FROM users
GROUP BY email
HAVING count( email ) >1
)t2
LEFT JOIN users t1 ON t1.email = t2.email
Thanks!



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.