mySQL hierarchical grouping sort

后端 未结 3 993
暗喜
暗喜 2021-01-15 01:02

I have a schema that essentially looks like this:

CREATE TABLE `data` (
  `id` int(10) unsigned NOT NULL,
  `title` text,
  `type` tinyint(4),
  `parent` int         


        
3条回答
  •  失恋的感觉
    2021-01-15 01:37

    Here's a solution tested to work on SQL Server. Should be essentially the same on MySQL

    select Id, Title, [Type], Id as OrderId from Hier h1 where [Type] = 1
    union
    select Id, Title, [Type], Parent as OrderId from Hier h2 where [Type] = 2
    order by OrderId, [Type]
    

提交回复
热议问题