Dear all, I've a little problem with a mysql statement. I want to insert a new column, with a record from an other table: table1: id username table2: name userid (new column) -> int, default value 0 What i want to do is compare table1.username with table2.name and insert table1.id into table2.userid if they match. And if there is no match, the userid table can be empty. Its a query I run 1 time to get the tables oke Someone that can help me with the statement to accomplish this?
You should be able to use an inner join to accomplish this. Inner join will only return rows where the condition in the join is satisfied. Something like this (untested): UPDATE table2 INNER JOIN table1 ON table2.name = table1.username SET table2.userid = table1.id;