本文介绍了sql父子关系查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的客户表为 CustomerID帐号Number ParentCustomerID --------- ------------- ----------------- 从中我如何才能找到拥有最多孩子的客户?
I am having a customer table as CustomerID AccountNumber ParentCustomerID - -------- ------------- ----------------- from this how can i find the customer which has most number of children ?
推荐答案您可以使用左外连接声明" 选择顶部1个C.CustomerId,IsNull(Count(P.*),0) 来自客户C 左外加入YOUR_PARENT_TABLE P开(A.ParentCustomerID = P.ParentCustomerID) 按IsNull(Count(P.*),0)desc 排序 如果要查看所有记录,请省略前1个 You can use Left Outer Join Statment Select top 1 C.CustomerId,IsNull(Count(P.*),0) From CUSTOMER C Left Outer Join YOUR_PARENT_TABLE P On ( A.ParentCustomerID= P.ParentCustomerID) Order By IsNull(Count(P.*),0)desc omit top 1 if you want to see all record
更多推荐
sql父子关系查询
发布评论