I want to insert a list of names to a table. I need to verify if the first name and last name entered already exists and if they don't, not to insert. The first name and last name are not indexes on the table. Currently I do a select statement looking for the first and last name and if the count = 0 then I'll insert the names. This is very inefficient since that is one select statement for every name. The size of the list can very so this will be very slow for large lists. Any help will be greatly appreciated.
Help in mysql insert
Moderator: General Moderators
Help in mysql insert
I want to insert a list of names to a table. I need to verify if the first name and last name entered already exists and if they don't, not to insert. The first name and last name are not indexes on the table. Currently I do a select statement looking for the first and last name and if the count = 0 then I'll insert the names. This is very inefficient since that is one select statement for every name. The size of the list can very so this will be very slow for large lists. Any help will be greatly appreciated.
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I think if you do a query like:
SELECT CONCAT(fname,lname) AS name, count( lname ) AS found
FROM mytable
GROUP BY lname,fname
WHERE (fname="fname1" and lname="lname1")
OR (fname="fname2" and lname="lname2");
you will get data back like:
johndoe 0
janedoe 0
bobsmith 0
From that you can come up with a list of names to insert.
SELECT CONCAT(fname,lname) AS name, count( lname ) AS found
FROM mytable
GROUP BY lname,fname
WHERE (fname="fname1" and lname="lname1")
OR (fname="fname2" and lname="lname2");
you will get data back like:
johndoe 0
janedoe 0
bobsmith 0
From that you can come up with a list of names to insert.