user198989
user198989

Reputation: 4665

mySQL - Using two JOINs in one query?

I am trying to use two JOIN statements in one query,

$sqlsorgu = mysql_query("SELECT *, COUNT(*), AVG(clicks), AVG(scrolls), AVG(spent)
FROM track where referid='".$memberr."' GROUP BY referer ORDER BY id desc limit 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 5) x 
   join (select count(*) total from track) y
) tc on t.id = tc.id") or die(mysql_error());

but I am getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN ( select id, country, num, num*100/total pct from (SELECT id,country' at line 1

What is the correct way to use it ?

Upvotes: 0

Views: 103

Answers (1)

munch1324
munch1324

Reputation: 1148

GROUP BY/ WHERE/ Order by come after join statements. Try reording like:

"SELECT *, COUNT(*), AVG(clicks), AVG(scrolls), AVG(spent)
FROM track t
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 5) x 
   join (select count(*) total from track) y
) tc on t.id = tc.id
where referid='".$memberr."' 
GROUP BY referer 
ORDER BY tc.id desc limit 15 

Upvotes: 1

Related Questions