Page 1 of 1

Help in mysql insert

Posted: Wed Oct 27, 2004 5:00 pm
by virdei
:?:

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.

Posted: Wed Oct 27, 2004 5:05 pm
by kettle_drum
If there is NEVER going to be a person with the same first and last names then you can simply use them in a single field as the primary key as it will always be unique data.

If not then you have to do as you are doing and query the database to check.

Posted: Wed Oct 27, 2004 8:28 pm
by Christopher
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.