2013-04-28 69 views
0

我有两个没有任何问题的mySQL查询。第一个提取引用者,第二个计算国家%和数字。但是,我需要在第一个查询中包含国家%和数字,换句话说,我怎样才能将它们一起使用?是否可以在一个查询中使用这2个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 

回答

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 

见接受的答案,以this question获取更多信息。你将不得不调整你的选择清单,以免发生冲突。