我正在使用sqlite这些表是:/ g" k6 v* w, n: n
ID_TABLE POINTS_A_TABLE POINTS_B_TABLE id number id_a points_a id_a points_a-------------- ---------------- ---------------- smith smith ... gordon gordon 11 butch 3 butch 11 sparrow 255 sparrow white white 46等等。这些命令之后 6 a4 k4 T7 u, G$ E4 Jselect id, points_a_table.points_a,points_b_table.points_a,points_c_table.points_a,points_d_table.points_afrom id_tableleft join points_a_table on points_a_table.id_a = id_table.idleft join points_b_table on points_b_table.id_a = id_table.idleft join points_c_table on points_c_table.id_a = id_table.idleft join points_d_table on points_d_table.id_a = id_table.idgroup by id我得到了这个结果,我在每一行都有一个id和与该id关联的点。+ ~1 Q/ n, n! m1 I+ Z8 N
, n& j7 F* v# ? N+ Q现在,我想 按行 获取 平均点, 并按平均降序排列。我想要的是: ! E5 n0 l1 ?, J- l: q5 Ssparrow| 56 white | smith | 33 butch | 24 gordon | 11 [11/1]我怎样才能做到这一点? : t0 N- P. @+ Z! @. B7 ] $ A8 u" ~0 ?0 v. T0 {- }3 M解决方案: 8 ^4 \! _ |0 i! f 若将所有点表混合在一起,则可简单计算每组的平均值: - H4 W1 }! z: Z* O/ b4 g5 X' d' Z NSELECT id, avg(points_a)FROM (SELECT id_a AS id,points_a FROM points_a_table UNION ALL SELECT id_a AS id,points_a FROM points_b_table UNION ALL SELECT id_a AS id,points_a FROM points_c_table UNION ALL SELECT id_a AS id,points_a FROM points_d_table)GROUP BY idORDER BY avg(points_a) DESC;