Checking if data exists in 2 tables

时间:2019-01-07 13:41:40

标签: php mysql join

I have 2 database tables _USERS and _NODES I would like to check _USERS and make sure every user has a record in _NODES.

At the moment i simply loop the _USERS table and insert a record into _NODES if it doesn't exist.

As I get more users this loop takes longer.... How can i check _USERS and insert any missing user ID's into _NODES without having to loop the records in _USERS...

function blc_adm_sync(){
global $wpdb;
$table = $wpdb->prefix."users";
$result = $wpdb->get_results("SELECT * FROM $table");
foreach($result as $mb8wp_config){

    $usrnode = getUNode($mb8wp_config->ID);

}
echo "All done, refresh your page.";

wp_die();   
}

This function simply loops the _USERS table and adds a record to _NODES if the user doesn't have a record in there already.

1 个答案:

答案 0 :(得分:0)

这很有效……

SELECT * FROM mbc1co_users u LEFT JOIN mbc1co_mb8wp_node_users n ON n.wpuid = u.ID WHERE n.wpuid IS NULL