GROUP BY - do not group NULL

后端 未结 6 1021
名媛妹妹
名媛妹妹 2020-12-05 03:48

I\'m trying to figure out a way to return results by using the group by function.

GROUP BY is working as expected, but my question is: Is it possible to have group b

6条回答
  •  渐次进展
    2020-12-05 04:27

    Maybe faster version of previous solution in case you have unique identifier in table1 (let suppose it is table1.id) :

    SELECT `table1`.*, 
        GROUP_CONCAT(id SEPARATOR ',') AS `children_ids`,
        IF(ISNULL(ancestor),table1.id,NULL) as `do_not_group_on_null_ancestor`
    FROM `table1` 
    WHERE (enabled = 1) 
    GROUP BY `ancestor`, `do_not_group_on_null_ancestor`
    

提交回复
热议问题