mySQL - 在一个查询中使用两个JOIN?

时间:2013-04-28 22:45:31

标签: php mysql sql

我试图在一个查询中使用两个JOIN语句,

$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());

但是我收到了这个错误:

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'JOIN附近使用正确的语法(从第1行的SELECT SELECT,country'中选择id,country,num,num * 100 / total pct

使用它的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

GROUP BY / WHERE /在加入声明之后命令。尝试录制如下:

"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