I am rather new to MySQL, I have this problem, which I have searched for help with, but could not find the answer (possibly because I am not sure what to search for). I am sure there is a simple solution.
I have a simple MySQL table like this:
id | referenceid | status
40 104702 4
39 104720 2
38 104720 0
37 104719 2
36 104719 0
35 104702 2
34 104702 0
41 104719 5
I want to fetch an array that will produce the following output:
For each unique “referenceid” field, I want to know the highest “ID” (ie most recent) and its “Status”.
So I have tried this query:
$result = mysql_query(
"SELECT status,id,referenceid
FROM image_status_tracking
GROUP BY referenceid",$db);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$status = $row['status'];
$referenceid = $row['referenceid'];
echo "ID $id, REFID $referenceid, Status $status <br />";
}
which gives this output:
ID 40, REFID 104702, Status 4
ID 37, REFID 104719, Status 2
ID 39, REFID 104720, Status 2
But this is not the result I was hoping for. The ID and Status for referenceid 104719 should be 41 and 5, not 37 and 2.
I cannot work out how to get the most recent ID line output for each referenceid.
Something I noticed, in PHPMYADMIN, (and you can see in the table above), the IDs are listed in reverse order, with the latest ID at the bottom. I am not sure if that is relevant to this problem.
I hope this is clear, I am sure it is a simple matter.
Thank you for reading.
James
Typing from memory so you might have to correct the syntax a little bit but the concept, i think, should work.