with t1 as
(select 1 id, 'zhangsan' username
from dual
union all
select 2 id, 'lisi' username
from dual
union all
select 3 id, 'wangwu' username
from dual
union all
select 4 id, 'zhaoliu' username
from dual),
t2 as
(select 1 ower_id, 2 friend_id
from dual
union all
select 1 ower_id, 3 friend_id
from dual
union all
select 2 ower_id, 3 friend_id
from dual
union all
select 3 ower_id, 4 friend_id
from dual)
select t1.username
from t1,
(select distinct friend_id
from t2
start with ower_id = 1
connect by ower_id = prior friend_id) t2
where t1.id = t2.friend_id;
(select 1 id, 'zhangsan' username
from dual
union all
select 2 id, 'lisi' username
from dual
union all
select 3 id, 'wangwu' username
from dual
union all
select 4 id, 'zhaoliu' username
from dual),
t2 as
(select 1 ower_id, 2 friend_id
from dual
union all
select 1 ower_id, 3 friend_id
from dual
union all
select 2 ower_id, 3 friend_id
from dual
union all
select 3 ower_id, 4 friend_id
from dual)
select t1.username
from t1,
(select distinct friend_id
from t2
start with ower_id = 1
connect by ower_id = prior friend_id) t2
where t1.id = t2.friend_id;
select t1.username
from t1,
(select distinct friend_id
from t2
start with ower_id = 1
CONNECT BY NOCYCLE ower_id = prior friend_id) t2
where t1.id = t2.friend_id;
楼主可以到mysql区去问问看
直至返回结果为空,即输出A表中的名单即可。