Help in mysql insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
virdei
Forum Newbie
Posts: 8
Joined: Mon Sep 13, 2004 10:02 am

Help in mysql insert

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
Post Reply