0

我有两个 mySQL 查询可以正常工作。第一个获取推荐人,第二个计算国家/地区的百分比和数字。但是,我需要在第一个查询中包含国家/地区百分比和数字,换句话说,我怎样才能将它们一起使用?

SELECT *, COUNT(*) FROM track 
where referid='".$member."'
GROUP BY referer 
ORDER BY id desc limit 0,15


select id, country, num, num*100/total pct 
from (SELECT id,country, count(*) as num 
FROM track GROUP BY country 
ORDER BY num desc limit 3) x 
join (select count(*) total from track) y
4

1 回答 1

2

加入内联视图:

SELECT *, COUNT(*) FROM track t
where t.referid='".$member."'
GROUP BY t.referer 
ORDER BY t.id desc limit 0,15
JOIN
(
   select id, country, num, num*100/total pct 
   from (SELECT id,country, count(*) as num 
   FROM track GROUP BY country 
   ORDER BY num desc limit 3) x 
   join (select count(*) total from track) y
) tc on t.id = tc.id

有关更多信息,请参阅此问题的已接受答案。而且您将不得不调整您的选择列表,以免发生冲突。

于 2013-04-28T02:20:05.097 回答