我有这个 mysql 查询,它给了我 ID = 5 时的所有子元素,现在我想按 ID 对结果进行分组并计算每个级别有多少个元素 = 5
`SELECT t1.ID, t1.name,
t1.ID as level1, t1.introID as i1,
t2.ID as level2, t2.introID as i2,
t3.ID as level3, t3.introID as i3,
t4.ID as level4, t4.introID as i4,
t5.ID as level5, t5.introID as i5,
t6.ID as level6, t6.introID as i6,
t7.ID as level7, t7.introID as i7,
t8.ID as level8, t8.introID as i8,
t9.ID as level9, t9.introID as i9,
t10.ID as level10, t10.introID as i10
FROM members AS t1
LEFT JOIN members AS t2 ON t2.parentID = t1.ID
LEFT JOIN members AS t3 ON t3.parentID = t2.ID
LEFT JOIN members AS t4 ON t4.parentID = t3.ID
LEFT JOIN members AS t5 ON t5.parentID = t4.ID
LEFT JOIN members AS t6 ON t6.parentID = t5.ID
LEFT JOIN members AS t7 ON t7.parentID = t6.ID
LEFT JOIN members AS t8 ON t8.parentID = t7.ID
LEFT JOIN members AS t9 ON t9.parentID = t8.ID
LEFT JOIN members AS t10 ON t10.parentID = t9.ID
LEFT JOIN members AS t11 ON t11.parentID = t10.ID
WHERE t1.ID = 5
group by t2.id, t3.id, t4.id, t5.id, t6.id, t7.id, t8.id, t9.id, t10.id`
这会产生以下结果:
ID NAME LEVEL1 I1 LEVEL2 I2 LEVEL3 I3 LEVEL4 I4
5 Vina Malik 5 0 14 0 41 0 122 0
5 Vina Malik 5 0 14 0 41 0 123 0
5 Vina Malik 5 0 14 0 41 0 124 0
5 Vina Malik 5 0 14 0 42 0 125 0
5 Vina Malik 5 0 14 0 42 0 126 0
5 Vina Malik 5 0 14 0 42 0 127 0
5 Vina Malik 5 0 14 0 43 0 128 5
5 Vina Malik 5 0 14 0 43 0 129 0
5 Vina Malik 5 0 14 0 43 0 130 0
5 Vina Malik 5 0 15 0 44 0 131 0
5 Vina Malik 5 0 15 0 44 0 132 0
5 Vina Malik 5 0 15 0 44 0 133 0
5 Vina Malik 5 0 15 0 45 5 134 0
5 Vina Malik 5 0 15 0 45 5 135 0
5 Vina Malik 5 0 15 0 45 5 136 5
5 Vina Malik 5 0 15 0 46 0 137 0
5 Vina Malik 5 0 15 0 46 0 138 0
5 Vina Malik 5 0 15 0 46 0 139 0
5 Vina Malik 5 0 16 0 47 0 140 0
5 Vina Malik 5 0 16 0 47 0 141 0
5 Vina Malik 5 0 16 0 47 0 142 0
5 Vina Malik 5 0 16 0 48 0 143 5
5 Vina Malik 5 0 16 0 48 0 144 0
5 Vina Malik 5 0 16 0 48 0 145 0
5 Vina Malik 5 0 16 0 49 5 146 0
5 Vina Malik 5 0 16 0 49 5 147 0
5 Vina Malik 5 0 16 0 49 5 148 0
我想在每个 i 列中获得等于 5 的元素计数,并对每个 id 的结果进行分组,然后从 select 中删除 ID,计算每个 i 列中有多少个元素 = 5 的最终标准是这个,在 i1 和i2 没有元素,但在 i3 和 i4 中,我们可以在 LEVEL3 列中看到具有 introID = 5 的子元素的 ID,在这种情况下,我们可以重复地看到元素 45 和 49,其值为 i4 = 5,但是在计数时当现实情况是 I3 列上必须是 2 时,它可能会导致得到 6
ID NAME LEVEL1 I1 LEVEL2 I2 LEVEL3 I3 LEVEL4 I4
5 Vina Malik 5 0 14 0 43 0 128 5
5 Vina Malik 5 0 15 0 45 5
5 Vina Malik 5 0 15 0 45 5
5 Vina Malik 5 0 15 0 45 5 136 5
5 Vina Malik 5 0 16 0 48 0 143 5
5 Vina Malik 5 0 16 0 48 0
5 Vina Malik 5 0 16 0 48 0
5 Vina Malik 5 0 16 0 49 5
5 Vina Malik 5 0 16 0 49 5
5 Vina Malik 5 0 16 0 49 5
所以最终的结果必须是这样的:
ID NAME I1 I2 I3 I4
5 Vina Malik 0 0 2 3
谁能帮我对所有内容进行分组和计数以获得所需的结果?谢谢