Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Check for duplication in DB
Old 08-05-2008, 03:57 PM Check for duplication in DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
I have a few ideas rolling around in the back of my head, but what do you guys think is the best way to check for duplication in a sql db? Just check to see if there are any rows where NAME is the same. Its getting to be a decent sized database so I figured I would ask the experts so its a little more efficient.

Thanks!
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-05-2008, 08:31 PM Re: Check for duplication in DB
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Code:
SELECT primary_key_id, count(name) as total_uses FROM table_name WHERE total_uses > 1
I believe will do it for you. The use of the total_uses alias may fail -- I didn't test that, but it does give the overall idea.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-05-2008, 09:50 PM Re: Check for duplication in DB
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
almost Jeremy...
It's not where total_uses>1 but "having total_uses>1".
And often, you will need to do a "group by"
Code:
select count(name) as cnt
from tbl
having count(name)>1
group by name
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-05-2008, 09:55 PM Re: Check for duplication in DB
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Thanks Tripy. I always have a hell of a time when I alias something and need to refer to it. Never had a chance to formally study DB's and SQL in college. Have just hacked my way through MySQL (but, to be fair, I don't do bad hacking MySQL's language structure ).

EDIT: Oh, I added a primary key field so that the results can be cross-referenced.

DOUBLE-EDIT: Ah, but the primary key won't work there because of the grouping you suggest, so one would need to select the field being grouped on too, right?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 08-05-2008 at 09:56 PM..
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-05-2008, 10:20 PM Re: Check for duplication in DB
vn5ltr's Avatar
Skilled Talker

Posts: 93
Location: Melbourne, Australia
Trades: 0
The HAVING should come after the GROUP BY.
vn5ltr is offline
Reply With Quote
View Public Profile
 
Old 08-06-2008, 01:49 PM Re: Check for duplication in DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Wow good thing I asked. I was thinking of some crazy scheme using arrays and sorting the arrays and.... well it wouldve been a mess.

Thanks!
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 08-08-2008, 05:19 PM Re: Check for duplication in DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
So.... what do I do next?

I have this
Code:
$result=mysql_query("select count(name) as cnt from mreleased group by name having count(name)>1") or die(mysql_error());
while($row = @mysql_fetch_array($result))
	{ 
	echo $row['name']." - ".$row['idnum'];
	}
It works fine, but it wont output anything except for the "-"
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 08-08-2008, 05:25 PM Re: Check for duplication in DB
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
It's not showing those entries b/c you're not selecting them.

BTW: Handling errors is better than dying.

Try this:
PHP Code:
if ($result=mysql_query("select name, idnum, count(name) as cnt from mreleased group by name having count(name)>1"))
  if (
mysql_num_rows($result) > 0) {
    while(
$row = @mysql_fetch_array($result)) { 
      echo 
$row['name']." - ".$row['idnum'].' had '.$row['cnt'].' entries';
    }
  }
}
if (
mysql_errno() != 0) {
  echo 
'Error executing query.  MySQL said "'.mysql_error().'".';

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-08-2008, 05:27 PM Re: Check for duplication in DB
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Oh crap. You're grouping, so ids are probably not available. Remove references to idnum:
PHP Code:
if ($result=mysql_query("select name, count(name) as cnt from mreleased group by name having count(name)>1"))
  if (
mysql_num_rows($result) > 0) {
    while(
$row = @mysql_fetch_array($result)) { 
      echo 
$row['name'].' had '.$row['cnt'].' entries';
    }
  }
}
if (
mysql_errno() != 0) {
  echo 
'Error executing query.  MySQL said "'.mysql_error().'".';

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-08-2008, 09:35 PM Re: Check for duplication in DB
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Incredible. Thanks so much for the help. This is clearly beyond my current capabilities . Thanks again!
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Check for duplication in DB
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30025 seconds with 12 queries