0

I have two mySQL queries that work without any problems. The first one fetches referers, and the second one calculates the country %'s and numbers. However, I need to include the country %'s and numbers in the first query, in other words, how can I use them together ?

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
user198989
  • 4,574
  • 19
  • 66
  • 95

1 Answers1

2

Join with an inline view:

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

See the accepted answer to this question for more info. And you will have to tweak your select lists so that there are no conflicts.

Community
  • 1
  • 1
cdonner
  • 37,019
  • 22
  • 105
  • 153