是否可以在一个查询中使用这两个mySQL查询?

时间:2013-04-28 02:14:44

标签: php mysql

我有两个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

1 个答案:

答案 0 :(得分: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的已接受答案。而且你必须调整你的选择列表,以便没有冲突。