简体   繁体   English

在子查询中使用查询列

[英]Use column from query in subquery

I've got a table with user data and every user has it's parent user.我有一个包含用户数据的表,每个用户都有它的父用户。 In my last question I wanted to show child users from two levels for current user, now I need to show table with count of the users in each level for every user.在我的最后一个问题中,我想为当前用户显示两个级别的子用户,现在我需要显示每个用户每个级别的用户数表。 I thought I could just edit the query from last question, but I can't get it to work.我以为我可以编辑上一个问题中的查询,但我无法让它工作。 How can I use a column from select statement in a subquery?如何在子查询中使用 select 语句中的列?

My query:我的查询:

SELECT c.id, c.nickname, c2.u1, c2.u2 
FROM favor_customer c, (
SELECT count(*) as u1 
FROM favor_customer u 
WHERE u.parent_id = c.id 
UNION ALL 
SELECT count(*) as u2 
FROM favor_customer u JOIN favor_customer uc ON uc.parent_id = u.id 
WHERE u.parent_id = c.id) c2

I'm getting Unknown column 'c.id' in 'where clause'Unknown column 'c.id' in 'where clause'收到Unknown column 'c.id' in 'where clause'

Sample data:样本数据:

id  | nickname | parent_id
1   |   AAA    |   null
2   |   BBB    |    1
3   |   CCC    |    2
4   |   DDD    |    2

Desired output:期望的输出:

id  | nickname | level_1 | level_2
1   |   AAA    |    1    |   2 
2   |   BBB    |    2    |   0
3   |   CCC    |    0    |   0
4   |   DDD    |    0    |   0

level_1 and level_2 columns are count of users in each level. level_1 和 level_2 列是每个级别的用户数。 What am I doing wrong?我究竟做错了什么?

I am thinking outer join and aggregation:我在考虑外连接和聚合:

select fc.id, fc.nickname,
       count(distinct fc1.id) as level1,
       count(distinct fc2.id) as level2
from favor_customer fc left join
     favor_customer fc1
     on fc1.parent_id = fc.id left join
     favor_customer fc2
     on fc2.parent_id = fc1.id
group by fc.id, fc.nickname;

Sure this can be done by repeated joins, but when it comes to hierarchical data, recursive queries are a tool that's better suited for the job当然这可以通过重复连接来完成,但是当涉及到分层数据时,递归查询是一种更适合这项工作的工具

with recursive customer_relation AS
  (
  select id, 
      parent_id ancestor_id, 
      1 distance
  from favor_customer
  where parent_id is not null
  union all
  select c.id, r.ancestor_id, distance+1
  from favor_customer c inner join customer_relation r on r.id = c.parent_id
  )
select c.id, 
    c.nickname, 
    (select count(*) from customer_relation where ancestor_id = c.id and distance = 1) level_1,
    (select count(*) from customer_relation where ancestor_id = c.id and distance = 2) level_2
from favor_customer c

note: you need the subselects (which can be replaced by joins & grouping) because of your requirement to have the level_1, level_2 in their own columns.注意:您需要子选择(可以用连接和分组替换),因为您需要在它们自己的列中包含 level_1、level_2。 If what you needed was a combination id-level-count, this becomes even more elegant.如果您需要的是组合 id-level-count,这将变得更加优雅。

Fiddle for experimenting实验用小提琴

MySQL recursive CTE tutorial MySQL递归CTE教程

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

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