我有一个表格,其中我用父参考
存储会员数据如果HU1是父ID我是指spos如何计算HU1下的中位数与嵌套子中位数
例如,看到HU5在HU1下,而HU6在HU5下,那么我如何计算在父HU1下的总嵌套MID 这里HU1的总计数是10,我如何从sql查询
获得这个我使用mysql 5.0
我试过了
SELECT parent.mid, COUNT(child.mid) AS child_count FROM member parent INNER JOIN member child ON child.spos = parent.mid WHERE child.spos = 'HU1' GROUP BY parent.mid;
但它只显示2而不是10
和
SELECT mid,spos,(SELECT count(unit) from member where spos=spos and spos='HU1')as totaspos from member where spos='HU1'
它也只显示2而不是10
答案 0 :(得分:0)
我会在数据库之外以编程方式执行此操作,获取整个表或新行,直到找不到子项。
可以假设树的已知最大深度,但这是一个非常昂贵的操作。你基本上必须加入表格,与最大深度相同的次数。
答案 1 :(得分:0)
这是另一个想法......
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(spos VARCHAR(12) NOT NULL PRIMARY KEY
,lft INT NOT NULL
,rgt INT NOT NULL
);
INSERT INTO my_table VALUES
('HU1' ,1,22),
('HU2' ,2,3),
('HU3' ,4,5),
('HU4' ,6,7),
('HU5' ,8,21),
('HU6' ,9,20),
('HU7' ,10,19),
('HU8' ,11,18),
('HU9' ,12,17),
('HU10',13,16),
('HU11',14,15);
SELECT COUNT(*)
FROM my_table x
JOIN my_table y
ON y.lft > x.lft
AND y.rgt < x.rgt
WHERE x.spos = 'HU1';
+----------+
| COUNT(*) |
+----------+
| 10 |
+----------+
1 row in set
答案 2 :(得分:0)
不使用SQL查询,但我已经使用函数进行了管理 我想分享整个页面代码,希望这会帮助某人
<!--#include file="mrraja.asp"-->
<style>
.propertyDetails td{text-align: left; padding-left: 1em;width: 10%;}
a{cursor: pointer;}
</style>
<div class="container">
<div class="col-md-12 wow fadeInLeft" data-wow-delay="0.4s">
<div class="living_box" style="height: 30em;overflow: auto;">
<%
Set allspos=TheDB.Execute("SELECT COUNT(*) as totdir FROM member where spos='"&iam("mid")&"' ORDER BY sl asc")
if not allspos.eof=true then
totdirme=allspos("totdir")
else
totdirme=0
end if
%>
<div class="living_desc">
<a href="#" class="btn3">Full Team Detail</a>
<p class="price">Total Directs: <%=totdirme%></p>
</div>
<table border="1" class="propertyDetails">
<tbody>
<tr>
<td>MID</td>
<td>NAME</td>
<td>SPONSOR</td>
<td>MOBILE</td>
<td>ACTIVE</td>
<td>COUNT</td>
</tr>
<%
Dim countrec:countrec=0
Dim countunit:countunit=0
Dim unt:unt=0
Dim tunt:tunt=0
call GET_HASAN(CStr(iam("mid")))
'===============
Public Sub GET_HASAN(MRRAJA)
sel = " SELECT mid FROM member where spos='" & MRRAJA & "' order by mid asc "
Set rs = TheDB.Execute(sel)
Do while Not rs.eof=true
If Not rs.EOF = True Then
l = rs.Fields(0)
Else
l = 0
End If
Call GET_IDEA1((l))
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
'------------------------------
Public Sub GET_IDEA1(MRRAJA)
call SET_ME(CStr(MRRAJA))
sel = " SELECT mid FROM member where spos='" & MRRAJA & "' order by mid asc "
Set rs = TheDB.Execute(sel)
Do while Not rs.eof=true
If Not rs.EOF = True Then
l = rs.Fields(0)
Call GET_IDEA1((l))
Else
l = 0
levelL=levelL+1
End If
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
'===============
Public Sub SET_ME(MRRAJA)
sel = " SELECT * FROM member where mid='" & MRRAJA & "' order by mid asc "
Set rs = TheDB.Execute(sel)
If Not rs.EOF = True Then
mid1=cstr(rs("mid"))
fname=rs("fullname")
snme=rs("spos")
sps=rs("spos")
mob=rs("mob")
address=(rs("addrs"))+" "+(rs("city"))+" "+(rs("dist"))
pkg=rs("act")
if pkg="YES" then
countunit=1
unt=unt+rs("unit")
else
countunit=0
unt=unt
end if
tunt=tunt+rs("unit")
mob=rs("mob")
End if
rs.close
Set rs = Nothing
%>
<tr>
<td><%=mid1%></td>
<td><%=fname%></td>
<td><%=snme%></td>
<td><%=mob%></td>
<td><%=pkg%></td>
<td><%=countunit%></td>
</tr>
<%End Sub%>
<tr>
<td colspan="2">Total: <%=tunt%></td>
<td colspan="2">Active: <%=unt%></td>
<td colspan="2">Inactive: <%=tunt-unt%></td>
</tr>
</tbody></table>
</div>
</div>
</div>
</div></div>