时间:2021-07-01 10:21:17 帮助过:29人阅读
思路:
(SELECT COUNT(*)
FROM (SELECT DISTINCT sender_id, send_to_id
FROM friend_request) A)
(SELECT COUNT(*)
FROM (SELECT DISTINCT requester_id, accepter_id
FROM request_accepted) B)
最终代码
SELECT ROUND(IFNULL(
(SELECT COUNT(*)
FROM (SELECT DISTINCT requester_id, accepter_id
FROM request_accepted) B)
/
(SELECT COUNT(*)
FROM (SELECT DISTINCT sender_id, send_to_id
FROM friend_request) A)
,0) ,2) AS accept_rate;
思路:
采用UNION ALL将表的requester_id和accepter_id列上下拼接,并创建临时表A
用COUNT()统计每个requester_id出现的次数
将A按照requester_id降序排列
取第一行。
SELECT A.requester_id AS id, COUNT(A.requester_id) AS num
FROM(
SELECT requester_id
FROM request_accepted
UNION ALL
SELECT accepter_id
FROM request_accepted) A
GROUP BY A.requester_id
ORDER BY num DESC
LIMIT 1;
[SQL]597(表2行数/表1行数)+602(表的上下拼接)
标签:union 排列 mysql com mys des class 临时表 select