0

我试图在一个查询中使用两个 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 服务器版本相对应的手册,以在第 1 行的 'JOIN ( select id, country, num, num*100/total pct from (SELECT id,country' ) 附近使用正确的语法

正确的使用方法是什么?

4

1 回答 1

1

GROUP BY/ WHERE/ Order by come after join 语句。尝试重新排序:

"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 
于 2013-04-28T22:48:18.220 回答