(100分)请教个sql语句,谢谢
假设我有一个表,叫做member,这个member顾名思义,里面除了有id,name之外,还有一个介绍人ID(JieShaoRenID),就是说这个成员可能是老会员介绍的,我先在想做的是列出所有会员,以及他们都介绍了多少个人,原本的想法是:
select memberID,memberName,
(select count(*) from Members where JieShaoRenID= "Members ".memberID)
from Members
但发现哪个介绍人数总是0,想了下倒也明白了是问题(JieShaoRenID与他自己行的memberID比较去了),但到底如何让它与外面select中的memberID比较呢???
描述的有些乱!请教了,谢谢!!!
------解决方案--------------------select memberID,memberName,
(select count(*) from Members where JieShaoRenID=A.memberID and memberID <> A.memberID)
from Members as A
------解决方案--------------------select memberID,memberName,
(select count(*) from Members where JieShaoRenID=a.memberID)
from Members a
------解决方案--------------------select b.memberID,b.memberName,a.count from (select JieShaoRenID as memberID,count(*) as count from tablename group by JieShaoRenID)a,tablename b where a.memberID=b.memberID order by memberID