How can I select the ID of a row with the max value of another column in a query that joins multiple tables?
For example, say I have three tables. tblAccount which stores a grouping of users, like a family. tblUser which stores the users, each tied to a record from tblAccount. And each user can be part of a plan, stored in tblPlans. Each plan has a Rank column that determines it's sorting when comparing the levels of plans. For example, Lite is lower than Premium. So the idea is that each user can have a separate plan, like Premium, Basic, Lite etc..., but the parent account does not have a plan.
How can I determine the highest plan in the account with a single query?
tblAccount
| PKID | Name |
|---|---|
| 1 | Adams Family |
| 2 | Cool Family |
tblUsers
| PKID | Name | AccountID | PlanID |
|---|---|---|---|
| 1 | Bob | 1 | 3 |
| 2 | Phil | 2 | 2 |
| 3 | Suzie | 2 | 1 |
tblPlans
| PKID | Name | Rank |
|---|---|---|
| 1 | Premium | 3 |
| 2 | Basic | 2 |
| 3 | Elite | 4 |
| 4 | Lite | 1 |
Here's the result I'm hoping to produce:
| AccountID | Name | HighestPlanID | PlanName |
|---|---|---|---|
| 2 | Adams Family | 1 | Premium |
I've tried:
SELECT U.AccountID, A.Name, MAX(P.Rank) AS Rank, P.PKID as HighestPlanID, P.Name as PlanName
FROM tblPlans P
INNER JOIN tblUsers U ON U.PlanID = P.PKID
INNER JOIN tblAccounts A ON U.AccountID = A.PKID
WHERE U.AccountID = 2
and the query will not always work, selecting the MAX of Rank does not select entire row's values from tblPlans.
I am looking for a solution that is compatible with mysql-5.6.10