Reputation: 1641
claimId is foreign key
Table (statusTable)
Id_ _chaseStatus_ _claimId_
1 Open CL001
2 Close CL002
3 Open CL001
4 Open CL003
5 Open CL001
6 Open CL003
$query = "SELECT * FROM statusTable ";
$query .= "WHERE (`chaseStatus` = 'Open') ";
$query .= "AND (id = (SELECT MAX(id) FROM statusTable))";
while($row = mysqli_fetch_assoc($result)){
$items[] = $row;
}
//$items = array_reverse($items ,true);
foreach($items as $item){
$claimId = $item["claimId"];
echo $claimId;
}
My query gives me only one column which is highest id.
But I am trying to get only 'Open' from 'chaseStatus' for each 'claimId' (with highest id) like;
How can I get like this
for id = 5 : CL001
AND
for id = 6 : CL003
Any ideas?
Upvotes: 1
Views: 982
Reputation: 1269773
You can do what you want by including the 'Open'
condition in the subquery:
SELECT *
FROM statusTable
WHERE `chaseStatus` = 'Open' AND
id IN (SELECT MAX(id) FROM statusTable WHERE chaseStatus = 'Open' GROUP By ClaimId);
I think it is redundant to have the open
condition in the outer query, so this should work for you:
SELECT *
FROM statusTable
WHERE id IN (SELECT MAX(id) FROM statusTable WHERE chaseStatus = 'Open' GROUP BY ClaimId);
Upvotes: 0
Reputation: 6582
You can retrieve the highest id of the claimid using group by.
$query = "SELECT max(Id) as Id,claimId FROM statusTable ";
$query .= "WHERE (`chaseStatus` = 'Open') ";
$query .= "GROUP BY claimId";
THis should result in the following table
Id claimId
5 CL001
6 CL003
Here's a SQL Fiddle: http://sqlfiddle.com/#!2/b000e/1
Upvotes: 4