Jump to content

[SOLVED] how to check duplicate entries in mysql


pixeltrace

Recommended Posts

This is fairly straight forward....

 

if the table has a primary key then each record will be different.

 

what you need to do is check each entry againts every other enrty where the primary key is different but everything else is the same...

 

so for this example I have a table 'test' with 3 fields 'key','text' and 'stuff'...

 

$qry = "SELECT `test`.* FROM `test` AS `t1`, `test` AS `t2`WHERE `t1`.`key` != `t2`.`key` AND `t1`.`text` = `t2`.`text` AND `t1`.`stuff` = `t2`.`stuff`";
$qry = mysql_query($qry);
while($row = mysql_fetch_assoc($qry)
{
print_r($row);
echo "<br />";
}

 

This should print out all duplicates - make sure you only delete all but ONE of each entry - they will come in sets of 2 or more remember!!!

Link to comment
Share on other sites

hi,

 

i tried this codes, im not sure if this is correct.

<?php 

$dbhost = 'localhost';
$dbusername = 'username';
$dbpasswd = 'password';
$database_name = 'db';

	$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
	or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");


$qry = "SELECT `jos_muse_users`.* FROM `jos_muse_users` AS `t1`, `jos_muse_users` AS `t2`WHERE `t1`.`username` != `t2`.`email`";
$qry = mysql_query($qry);
while($row = mysql_fetch_assoc($qry)
{
print_r($row);
echo "<br />";
}
?>

 

currently i am getting a parsing unexpected '}' in line 18.

 

hope you could help me solve this.

i wanted to know if i have duplicated entries for the username, and email.

 

thanks so much!

Link to comment
Share on other sites

also

 

The SQL statement should be

 

<?php
$qry = "SELECT jos_muse_users.* FROM jos_muse_users AS t1, jos_muse_users AS t2 WHERE t1.username != t2.username AND t1.email != t2.email";
?>

 

EDIT:

Thats for users who have the same username and email in the table more than once..

it will not find duplicate emails if their username is different

 

 

WIll find

user1 user1@emails.com

user1 user1@emails.com

 

will NOT find

user1 user1@emails.com

user2 user1@emails.com

 

will NOT find

user1 user1@emails.com

user1 user2@emails.com

Link to comment
Share on other sites

Hi,

 

i am still getting an error

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/html/mumcentre/qry.php on line 17

 

this is my current code for the page

 

<?php 

$dbhost = 'localhost';
$dbusername = 'username';
$dbpasswd = 'password';
$database_name = 'db';

	$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") 
	or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");


$qry = "SELECT jos_muse_users.* FROM jos_muse_users AS t1, jos_muse_users AS t2 WHERE t1.username != t2.username AND t1.email != t2.email";
$qry = mysql_query($qry);
while ($row = mysql_fetch_assoc($qry))
{
print_r($row);
echo "<br />";
}
?>

 

hope you could help me with this.

also, let say there are duplicate entries, how can i delete the

duplicates? what is the code for this?

i found a Mysql Delete Duplicate entries software but its not working.

 

need help on this. thanks!

Link to comment
Share on other sites

Hi my problem is solved already and i found a solution which i wanted to share also

------------------

STEP 1

-	Create new table to have zero duplicates
mySQL Code 
--------------
CREATE TABLE new_table AS
SELECT *
FROM old_table
GROUP BY email
HAVING ( COUNT(email) = 1 )

STEP 2

-	Rename old_table to another name and rename new_table to old_table
mySQL Code 
--------------
(for backup purposes)
RENAME TABLE old_table TO old_table_old; 

RENAME TABLE new_table TO old_table;

 

Thanks again!

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.