vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Query to select duplicate fields (https://vborg.vbsupport.ru/showthread.php?t=150614)

TheMilkCarton 06-25-2007 04:28 PM

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!

Antivirus 06-25-2007 05:43 PM

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?

TheMilkCarton 06-25-2007 05:48 PM

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.

Farcaster 06-25-2007 11:18 PM

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]

TheMilkCarton 06-25-2007 11:46 PM

Well, something went screwy and I had to KILL the process. I just about had a panic attack.

Farcaster 06-26-2007 03:30 PM

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.

Eikinskjaldi 06-26-2007 08:56 PM

You could use group_concat

select username, group_concat(email), count(*) from user group by username having count(*) > 1

TheMilkCarton 06-28-2007 11:38 PM

Quote:

Originally Posted by Eikinskjaldi (Post 1277243)
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]


All times are GMT. The time now is 06:23 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01130 seconds
  • Memory Usage 1,726KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete