所以我有两个表,STOPS_AT和POINT_OF_INTEREST,它们是这样的:
STOPS_AT [ Route_ID,Stop_ID ]
和
POINT_OF_INTEREST [ Stop_ID,名称,类别]
现在我正在尝试选择在某个兴趣点拥有最多停靠点的Route_ID。换句话说,具有最多停靠次数的路线,也出现在POINT_OF_INTEREST表中。我尝试过以下方法:
select Route_ID
from (select Route_ID, count(POINT_OF_INTEREST.Stop_ID) as cnt
from STOPS_AT R
group by Route_ID
) rc join
(select max(cnt) as maxcnt
from (select Route_ID, count(POINT_OF_INTEREST.Stop_ID) as cnt
from STOPS_AT
group by Route_ID
) rc
) m
on rc.cnt = m.maxcnt;
然而,这似乎不起作用,说它不知道POINT_OF_INTEREST表?
答案 0 :(得分:0)
您的查询未加入point_of_interest
表格 - 这就是您收到错误的原因。您正在尝试计算您未选择的字段。
如果您不需要担心关系,一种简单的方法就是使用limit
:
select route_id
from (
select sa.route_id, count(*) cnt
from stops_at sa
join point_of_interest poi on sa.route_id = poi.route_id
group by sa.route_id
) t
order by cnt desc
limit 1
如果关系是关系,那么您可以使用上面的内容更改您的查询:
select Route_ID
from (
select sa.route_id, count(*) cnt
from stops_at sa
join point_of_interest poi on sa.route_id = poi.route_id
group by sa.route_id
) rc join
(select max(cnt) as maxcnt
from (
select sa.route_id, count(*) cnt
from stops_at sa
join point_of_interest poi on sa.route_id = poi.route_id
group by sa.route_id
) rc
) m
on rc.cnt = m.maxcnt;