Order row selection by multiple columns

2020-02-06 07:57发布

I have a database

id    |     parentid     |       name
1     |        0         |      CatOne
2     |        0         |      CatTwo
3     |        0         |      CatThree
4     |        1         |      SubCatOne
5     |        1         |      SubCatOne2
6     |        3         |      SubCatThree

How I can select this cats Order By id, parentid? That is

CatOne 1
--SubCatOne 4
--SubCatOne2 5
CatTwo 2
CatThree 3
--SubCatThree 6

5条回答
来,给爷笑一个
2楼-- · 2020-02-06 08:21

This should do it... with exception of a double dash "--" prefix to the name...

SELECT 
      t1.name,
      t1.id
   FROM 
      Table1 t1
   ORDER BY 
      case when t1.parentID = 0 then t1.ID else t1.ParentID end,
      case when t1.parentID = 0 then '1' else '2' end,
      t1.id

The order by FIRST case/when puts all the items that ARE the top level, or at the secondary level by the primary level's ID. So trying to use a parent * 1000 sample hack offered won't be an issue if you have over 1000 entries. The SECOND case/when will then force when the parent ID = 0 to the TOP of its grouped list and all its subsidiary entries UNDER it, but before the next parent ID.

however, if you DO want the double dash, change to

SELECT 
      if( t1.ParentID = 0, '', '--' ) + t1.name name,
     <rest of query is the same>
查看更多
▲ chillily
3楼-- · 2020-02-06 08:21

This:

select id as parentId,
0 as sortOrder,
id,
name
from cats 
where parentId = 0
union all
select parentId,
1 as sortOrder,
id,
name
from cats 
where parentId > 0
order by parentId, sortOrder, name

Returns:

ParentId sortOrder  id   Name
    1       0       1   CatOne
    1       1       4   SubCatOne
    1       1       5   SubCatOne2
    2       0       2   CatTwo
    3       0       3   CatThree
    3       1       6   SubCatThree
查看更多
做个烂人
4楼-- · 2020-02-06 08:22

assuming your table is named cats, try this:

select * from  cats
order by
      case when parentid = 0 then id else parentid end,
      case when parentid = 0 then 0 else id end

Updated to include when parent would have higher id compared to the children

查看更多
成全新的幸福
5楼-- · 2020-02-06 08:23

If you were to sort by: ORDER BY parentid, id

then you would get the order you are looking for, but it wouldn't be intended or anything, like your example.

SQL is probably not the best medium for doing indented group like that. You can...but it's better done in your front end app

edit: sorry misread question, what Eric Petroelje said.

edit edit: Or select from the table, joined back to itself, (one for the Cat and one for the SubCat) and then specify the different ordering, one from each table.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-02-06 08:28

You could try this. It is not exactly the format you specify but does it match what you require?

SELECT c.id,c.name,sc.id as subcatid,sc.name as subcatname
FROM cats c LEFT JOIN cats sc ON c.id=sc.parentid
WHERE c.parentid=0 order by c.id,sc.id;

It produces the following:

+------+----------+----------+-------------+
| id   | name     | subcatid | subcatname  |
+------+----------+----------+-------------+
|    1 | CatOne   |        4 | SubCatOne   |
|    1 | CatOne   |        5 | SubCatOne2  |
|    2 | CatTwo   |     NULL | NULL        |
|    3 | CatThree |        6 | SubCatThree |
+------+----------+----------+-------------+
查看更多
登录 后发表回答