Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 06-25-2007, 04:28 PM
TheMilkCarton TheMilkCarton is offline
 
Join Date: Jan 2007
Posts: 294
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Query to select duplicate fields

Hi, I have this:
[sql]
SELECT email,username,
COUNT(email) AS NumOccurrences
FROM user
GROUP BY email
HAVING ( COUNT(email) > 1 )
[/sql]

And it works just fine, but I was wondering how to make it so that it actually LISTS each occurence?

For example, it gives me this
Quote:
username_____email_________ numOccurrences
user1 -------- blah@blah.com -----3
otheruser1 ---- ***@***.com -----4
but I would like it to give me this:
Quote:
username_____email_________ numOccurrences
user1 -------- blah@blah.com -----3
user2 -------- blah@blah.com -----3
user3 -------- blah@blah.com -----3
otheruser1 ---- ***@***.com -----4
otheruser2 ---- ***@***.com -----4
otheruser3 ---- ***@***.com -----4
otheruser4 ---- ***@***.com -----4
This of course becomes redundant, and the "numOccurrences" column would be kind of unnecessary, but I'm really stupid with MySQL and can't seem to figure this out.

Thanks!
Reply With Quote
  #2  
Old 06-25-2007, 05:43 PM
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Location: Black Lagoon
Posts: 1,090
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

you're trying to find out how many times any unique email address appears in the db and list the different users that have those same email addresses?
Reply With Quote
  #3  
Old 06-25-2007, 05:48 PM
TheMilkCarton TheMilkCarton is offline
 
Join Date: Jan 2007
Posts: 294
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes. This is with a very old install of vBulletin, btw. I think vB 3.x is set up to never allow duplicate emails.

There are 35 emails that have been used at least twice, some up to 10 times, so I'd want a comprehensive list of the hundred or so usernames that have duplicate emails. The way I'm doing it now only shows me ONE of the usernames attached to the email.

I guess I could type out all 35 emails in a query? haha.
Reply With Quote
  #4  
Old 06-25-2007, 11:18 PM
Farcaster Farcaster is offline
 
Join Date: Dec 2005
Posts: 386
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm at work and I don't have access to a MySQL database at the moment, but I think this query will do it for you:

[SQL]SELECT
u.username, u.email, count(*) as NumOccurences

FROM
user u
INNER JOIN user u2 ON u.email = u2.email

GROUP BY
u.username, u.email

HAVING
count(*) > 1[/SQL]
Reply With Quote
  #5  
Old 06-25-2007, 11:46 PM
TheMilkCarton TheMilkCarton is offline
 
Join Date: Jan 2007
Posts: 294
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, something went screwy and I had to KILL the process. I just about had a panic attack.
Reply With Quote
  #6  
Old 06-26-2007, 03:30 PM
Farcaster Farcaster is offline
 
Join Date: Dec 2005
Posts: 386
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I ran this this morning on my own forum and it worked fine. It does do a table scan, I noticed. If you have a ton of users, you might try adding an index on the email field if there isn't one there already.
Reply With Quote
  #7  
Old 06-26-2007, 08:56 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You could use group_concat

select username, group_concat(email), count(*) from user group by username having count(*) > 1
Reply With Quote
  #8  
Old 06-28-2007, 11:38 PM
TheMilkCarton TheMilkCarton is offline
 
Join Date: Jan 2007
Posts: 294
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Eikinskjaldi View Post
You could use group_concat



select username, group_concat(email), count(*) from user group by username having count(*) > 1
Thanks so much. I had to switch it up a bit, but thanks for the right direction.

It ended up looking like:

[sql]SELECT email, group_concat( username ) , COUNT( email )
FROM user
GROUP BY email
HAVING (COUNT( email ) >1)[/sql]
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

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

Forum Jump


All times are GMT. The time now is 08:12 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04008 seconds
  • Memory Usage 2,223KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete