题目自寻:
最开始,读错题了。以为是被加好友最多的人。
后面才反应过来,是好友最多的人。也就是[被申请好友成功+申请别人好友成功] 最多的人。
思路和官方一样,就是统计出现在两个表里最多的数字,同时加上accept_date 不为null 的约束。
如何统计出现在两个表里最多的数字?
先union all 把两个表里都出现的数字,都查出来。不用union 是因为union会去重。
然后按照数字分组(group by ),count(*)统计,然后查最多的那个。
参考答案:
select ids as id,count(*) as num from (
select requester_id as ids from RequestAccepted
where accept_date is not null
union all
select accepter_id from RequestAccepted
where accept_date is not null
) h group by ids order by count(*) desc limit 1