简体   繁体   English

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

[英]mySQL - Using two JOINs in one query?

I am trying to use two JOIN statements in one query, 我正在尝试在一个查询中使用两个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());

but I am getting this error: 但我收到此错误:

You have an error in your SQL syntax; 您的SQL语法有误; 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 查看与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的(SELECT id,country)中选择“ JOIN(选择id,国家,num,num * 100 /总pct)

What is the correct way to use it ? 正确的使用方法是什么?

GROUP BY/ WHERE/ Order by come after join statements. 在联接语句之后使用GROUP BY / WHERE / Order by。 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM