WHERE not exist not working

So I am adding user data from a table to another for a WordPress’s plugin

$sql = mysql_query("INSERT INTO wp_bp_xprofile_data (user_id, field_id, value)
        SELECT ID, '7', cedula
FROM employees

WHERE NOT EXISTS(SELECT * 
                 FROM wp_bp_xprofile_data
                 WHERE (ID=user_id AND
                       '7'=field_id AND
                        cedula=value
                       )
                 )

The Query runs fine, but when ran a second/third… time, it duplicayes all the data I’m pulling from the employees table into xprofile. With the ‘Where Not Exists’ I want each employee data to be added only once if it doesn’t exist already.

This is weird and can’t really figure out what’s wrong, even in an early query for another table, it doesn’t duplicate the employees and works right, here I’m pulling data from wp_users into wp_usermeta:

    $sql2 = mysql_query("INSERT INTO wp_usermeta (user_id, meta_key, meta_value)
        SELECT ID, 'first_name', SUBSTRING_INDEX(display_name, ' ', 1)
FROM wp_users

WHERE NOT EXISTS(SELECT * 
                 FROM wp_usermeta
                 WHERE (ID=user_id AND
                       'first_name'=meta_key AND
                       SUBSTRING_INDEX(display_name, ' ', 1)=meta_value 
                       )
                 )

On this one, it won’t duplicate the records when ran again if the records are already in, if anyone can see what’s wrong, your help would be really appreciated.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s