kerry kerry - 3 months ago 12
MySQL Question

php / mysql query one table and update second table

A bit difficult to explain this one. I have a table of transactions which contains multiple entries of names. I want to scan this table and create a unique entry for each name in a second table. For instance table 1 contains :-

john smith
pete jones
sue james
john smith
john smith
pete jones
sue james
john smith

after the query I want table 2 to contain:-

john smith
pete jones
sue james

thanks for any advice

Answer

You got to use DISTINCT keyword in sql. It selects distinct values.

SELECT DISTINCT name from table_1 

And you will retrieve unique names. And all you have to do is to loop through in the search result and do an INSERT statement to insert to your new table.

Comments