sql--自链接(推荐人)

表1:

需求:查出推荐人,和被推荐人

1.通过group_concat函数和分组,查出每个id推荐的人有哪些

select group_concat(u_name, u_id) as referce_name, p_id from webusers group by p_id

2.查出pid的name

select referce_name, u_name
from (select group_concat(u_name, u_id) as referce_name, p_id from webusers group by p_id) a
         inner join webusers b
where a.p_id = b.u_id;

  

posted @ 2019-07-10 16:43  花花妹子。  阅读(255)  评论(0编辑  收藏  举报