使用SQL在查询时,我需要在结果中包括空白行,以便每组(family_id)等于4行. u9 r) @+ ~ x; r: Y2 J$ s
SELECT ROW_NUMBER() OVER(PARTITION BY family_id ORDER BY family_id) AS rowNum, full_name as name,family_idFROM tbl_person 7 I& b% q- b! q$ b, T; p 解决方案: " z6 T9 {1 @0 E4 \5 b1 |# F$ h
构建所需的空行并将其合并在一起,只需定义第二个查询。 - H- w, |) I1 ]4 gSELECT ROW_NUMBER() OVER(PARTITION BY family_id ORDER BY family_id) AS rowNum, full_name as name, family_idFROM tbl_personUNION ALLSELECT n.N AS rowNum, NULL AS name, p.family_idFROM (SELECT family_id,COUNT(*) family_count FROM tbl_person group by family_id) pINNER JOIN SELECT 1 AS N UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 ) n ON n.n > p.family_countORDER BY family_id, rowNum, name;