nickyfraggle
08-13-2009, 01:37 PM
Hello,
I'm trying to select a distinct field from my table called name.
But, each name entry has a unique ID so that I can identify it from all the other entries for that name.
So I want to run a query, that SELECTS DISTINCT name, but also gives me the unique ID. The unique ID would be different for every entry, so say my name was in the table 4 times, it would have 4 unique ID's.
I want the query to find the first instance of any name, return the value and it's ID, and disgard any entries it finds for the name after that.
Any ideas how I'd go about doing this? So far I have:
$query = mysql_db_query($db, "SELECT DISTINCT name FROM names01", $connection);
Which is fine, but it doesn't return an ID. I then tried:
$query = mysql_db_query($db, "SELECT DISTINCT name,id FROM names01", $connection);
But that didn't work because the id is always unique so it then returned all the entries attached to my name, not just 1. I also tried group by, but that doesn't let me use the id.
Is there a way to do this without 2 queries?
Thanks,
Nicky
I'm trying to select a distinct field from my table called name.
But, each name entry has a unique ID so that I can identify it from all the other entries for that name.
So I want to run a query, that SELECTS DISTINCT name, but also gives me the unique ID. The unique ID would be different for every entry, so say my name was in the table 4 times, it would have 4 unique ID's.
I want the query to find the first instance of any name, return the value and it's ID, and disgard any entries it finds for the name after that.
Any ideas how I'd go about doing this? So far I have:
$query = mysql_db_query($db, "SELECT DISTINCT name FROM names01", $connection);
Which is fine, but it doesn't return an ID. I then tried:
$query = mysql_db_query($db, "SELECT DISTINCT name,id FROM names01", $connection);
But that didn't work because the id is always unique so it then returned all the entries attached to my name, not just 1. I also tried group by, but that doesn't let me use the id.
Is there a way to do this without 2 queries?
Thanks,
Nicky