我想知道一个人的最大阵型,一个人可以有很多阵法条目。
表t_formation
idFormation | fkPerson | fkLevel | place
------------------------------------------------
1 | 1 | 2 | Oxford
2 | 2 | 1 | PlaySchool
3 | 1 | 3 | Trinity High
4 | 1 | 1 | My School
5 | 2 | 3 | My High
表a_level
idLevel | orderLevel | formation
-------------------------------------
1 | 1 | School
2 | 3 | University
3 | 2 | High school
我需要得到的是以下查询或所需的查询结果(每个人形成的最大订单级别以及他们研究该最大形成的地方)
fkPerson | maxOrderLevel | formation | place
----------------------------------------------------
1 | 2 | Univertity | Oxford
2 | 3 | High school | My High
为此,我使用 2 个子查询进行了查询,但无法创建有效的视图。
无地点查询SQL,得到每个人的最大编队
select fkPerson, a_level.orderLevel, a_level.formation
from (
select fkPerson, max(a_level.orderlevel) as ordermax
from t_formation left join a_level on t_formation.fkLevel = a_level.idLevel
group by fkPerson
) as form left join a_level on form.ordermax = a_level.orderlevel