Jump to content

help on sql query SELECT display


pixeltrace

Recommended Posts

hi,

 

i need help, i have a table that got lots of duplicates.

i need a query that will display all items with duplicates on the username

order by username where in the display will be something like:

 

=========

| id | username  | email

  1    username1 | email1@email.com

  2    username1 | email2@email.com

  3    username2 | email3@email.com

  4.  username2 | email4@email.com

 

hope you could help me with this.

 

thanks!

Link to comment
Share on other sites

Hmm, im a bit rusty on more advanced mysql functions...and im fairly sure this can be achieved in a single query. But you could try something like:

 

<?php
$sql = mysql_query("SELECT `username`,COUNT(`username`) FROM `yourtable` GROUP BY `username` HAVING COUNT(`username`) > 1") or die(mysql_error());
//this will find each of the duplicated values for the username, but (i think) will only return one of the values for it - e.g, in the results, you will only find username1 once - so we then cycle through the results to pull the rest of the info from the database
while($row = mysql_fetch_assoc($sql)){
$sql2 = mysql_query("SELECT * FROM `yourtable` WHERE `username`='$row[username]'") or die mysql_error());
while($row2 = mysql_fetch_assoc($sql2)){
echo "$row2[id] || $row2[username] || $row2[email] <br />";
}
}
?>

 

Im not 100% sure this'll work - i've hardly used group and having clauses, but its worth a go. As i say, someone else will probably be able to show you a single query for doing this too.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.