Jump to content

Mysql Ingnore Duplicate


tomtimms

Recommended Posts

I have 4 columns in my table.

 

ID, Name,Site,Status

 

Id is auto_incremented, I am trying to Insert new rows that will skip pass any duplicates.  Let's assume the database has 1 record already.

 

ID  Name  Site  Status

1  | Bob  |    1  | 4

 

I want to add (Bob|1|4) and then (Bob|2|4).  The first entry won't be inserted as its duplicated however the second will as Site is different.  I tried using INSERT IGNORE however whenever SITE was different it wouldn't INSERT the new record.

 

A name is associated to a site and is given a status, so I have different name's that can be assigned to different sites.  I hope this makes some sense.  Any help or starting point would be great. 

 

 

Link to comment
https://forums.phpfreaks.com/topic/200218-mysql-ingnore-duplicate/
Share on other sites

Firstly this is an SQL problem so should be on the other forum.

You would need to do something like

 

SELECT ID from MyTable WHERE Name = 'Bob and ' Site = 1 and Status = 4;
$query = mysql_query($sql);
$total = mysql_num_rows($query);
if($total == 0)
{
  // ok to go ahead and insert
}

Sorry that should have read

SELECTcount( ID) as Totla from MyTable WHERE Name = 'Bob' and  Site = 1 and Status = 4;
$query = mysql_query($sql);
$total = mysql_num_rows($query);
if($total == 0)
{  
  // ok to go ahead and insert
}

 

This is better

if $count == 0 no problem

if $count == 1 problem this already exsists

if $count >1 big problem this should not have happend report this to the webmaster !!!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.