我正在尝试从外部查询的每一行获取一个concat列表,但我在其他地方得到Unknown column 'outter_client' in 'where clause'
我看到你无法从子查询中访问外部变量,有没有办法重新编写这个查询没有存储过程,视图或临时表,并让它工作?
SELECT client AS outer_client, top_domain
FROM log
JOIN (
SELECT GROUP_CONCAT(t.domain) AS top_domain
FROM (
SELECT domain
FROM log
WHERE client = outer_client
GROUP BY 1
ORDER BY SUM(bytes)
LIMIT 5
) t
) k
GROUP BY client
ORDER BY SUM(bytes)
LIMIT 5;
答案 0 :(得分:1)
我认为您希望每个客户端的前五个域基于字节数。您可以使用嵌套选择和substring_index()
/ group_concat()
技巧来执行此操作:
select client,
substring_index(group_concat(domain order by sumbytes desc), ',', 5) as top5domains
from (select client, domain, sum(bytes) as sumbytes
from log
group by client, domain
) cd
group by client
order by sum(sumbytes) desc
limit 5;