6 M0 d' \* x, W5 ~( V# D解决方案: + c* j8 V6 k& g2 o3 H% |/ A# u 它没有测试,但我认为应该是MySQL中工作:; m" ]4 i) |; V* S+ A/ j
ORDER BY CASE WHEN parent_id=-1 THEN id ELSE parent_id END,created_at编辑: 如果你不能假设ID以与注释相同的逻辑顺序升序会变得更加复杂: ! J3 x; l3 C5 ^! v7 D: O" _9 eSELECT parent_id,id,created_at parent_date,null child_date,content FROM Comments WHERE parent_id=-1UNIONSELECT c.parent_id,c.id,p.created_at as parent_date,c.created_at as child_date,c.contentFROM Comments c JOIN (SELECT id,created_at,content FROM Comments WHERE parent_id=- GROUP BY id,created_at,content) p ON p.id=c.parent_idORDER BY parent_date,child_date