If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > INSERT or ignore?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-09, 08:07
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
INSERT or ignore?

I've got a table for a mailing list web application, containing a column for the email address and a column for the List ID.

I need to run through an uploaded CSV file, adding new records for new email addresses for the selected list ID.

I can only see a way to do this using two queries, one to see if there are any existing records and another to do the actual insert (if there aren't):

SELECT id FROM tblContacts WHERE listID = 123 and email = 'me@web.com'

INSERT INTO tblContacts (listID, email) VALUES (123, 'me@web.com')

Is there a way to do it with just the one?
Reply With Quote
  #2 (permalink)  
Old 02-11-09, 08:16
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Would this work ? :
Code:
INSERT INTO tblContacts (listID, email) 
select csv.listID,  csv.email
from   csvTable csv
where  not exists( 
         select 1
         from   tblContacts c
         where  c.listID = csv.listID
                and c.email = csv.email )
Reply With Quote
  #3 (permalink)  
Old 02-12-09, 04:04
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
I hadn't thought of doing it like that - my application currently reads CSV contents into an array and loops through it - but it's certainly an option; just dump the CSV into a temporary table and use that. Thanks
Reply With Quote
  #4 (permalink)  
Old 02-12-09, 04:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
yes, load the csv into a table

then run this --
Code:
INSERT IGNORE
  INTO Contacts
SELECT listID, email
  FROM CSVtable
Quote:
Originally Posted by da Manual
If you specify the IGNORE keyword in an INSERT statement, errors that occur while executing the statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the error is ignored and the row is not inserted.
just what you wanted
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-12-09, 05:24
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Aah... but that would depend on my existing tblContacts having the email address and list ID fields as the primary key, instead of the autoincrement integer field it's got already, right?

Me and my autoincrementing ID's
Reply With Quote
  #6 (permalink)  
Old 02-12-09, 06:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
all you would need is to add a UNIQUE constraint on (email,listid)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 02-12-09, 06:03
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by r937
insert IGNORE
Is insert IGNORE standard SQL or just proprietary to mysql?
I seem to remember you saying

and will he get warnings every time he runs this SQL?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On