按降序确定提供乘车服务的前 2 名首选用户
Identify the top 2 preferred users in descending order who offered rides
ride_users table 中的用户也是 table 列 ride_provider_id 中的乘车提供者。
我必须确定提供最大乘车次数的前两名用户作为乘车提供者。
我已经试过了,但没有得到想要的结果:
select distinct r1.user_id, u.first_name||' '||u.last_name as user_name
from user_details u
inner join ride_users r1 on u.user_id=r1.user_id
inner join ride r2 on r1.ride_id =r2.ride_id
where r1.user_id = r2.ride_provider_id
order by r1.user_id desc;
内联table会给出乘车次数,你可以用它来排序和限制。
SELECT u.*
FROM user_details u
INNER JOIN (SELECT r.ride_provider_id, count(*) rides
FROM ride r
GROUP BY r.ride_provider_id
) mr on mr.ride_provider_id = u.user_id
ORDER BY mr.rides DESC
LIMIT 2
select distinct r1.user_id, u.first_name||' '||u.last_name as user_name
from user_details u
inner join ride_users r1 on u.user_id=r1.user_id
inner join ride r2 on r1.ride_id =r2.ride_id
where r1.user_id = r2.ride_provider_id
order by r1.user_id desc;
内联table会给出乘车次数,你可以用它来排序和限制。
SELECT u.*
FROM user_details u
INNER JOIN (SELECT r.ride_provider_id, count(*) rides
FROM ride r
GROUP BY r.ride_provider_id
) mr on mr.ride_provider_id = u.user_id
ORDER BY mr.rides DESC
LIMIT 2