If I have a table with data like this :
Col1 | Col2 | Col3
111 | a |
222 | b | 1
111 | |
And I wish to select Distinct Col1 only from my temporary table then insert into another table.But with the table above , the second “111” will be selected as well since it has different Col2 data.
Or if I select Distinct Col1 only , the other columns data will be ignored…
So is there any way to solve this problem? I wish to choose only the first inserted row of same Col1 data.
I’m using this query:
mysql_query("INSERT DELAYED INTO Tableb (Col1,Col2,Col3) SELECT DISTINCT Col1,Col2,Col3 FROM TempTable");
Thanks for any reply.
PS: I’m not sure how to specified the title according to my question…perhaps someone can help me.
First you need to determine last ID per distinct value of col1, and then you select rows containing this id:
Inner query returns ID’s to be inserted, so one simply joins them to original table to filter the table.
Substitute max(id) with min(id) if you want first inserted TempTable record.