按多列排序行选择

发布于 2024-10-18 04:49:29 字数 493 浏览 2 评论 0原文

我有一个数据库

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

如何选择这只猫 Order By id, parentid?那是

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彡翼 2024-10-25 04:49:29

假设您的表名为 cats,请尝试以下操作:

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

更新以包括与子项相比,父项具有更高的 id

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

彩虹直至黑白 2024-10-25 04:49:29

这应该可以做到...除了名称的双破折号“--”前缀...

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

按第一个情况/何时的顺序将所有属于顶级的项目或按主要级别的次要级别ID。因此,如果您有超过 1000 个条目,尝试使用提供的父 * 1000 个示例 hack 不会有问题。第二个 case/when 将在父 ID = 0 时强制到其分组列表的顶部及其下方的所有子条目,但在下一个父 ID 之前。

但是,如果您确实想要双破折号,请更改为

SELECT 
      if( t1.ParentID = 0, '', '--' ) + t1.name name,
     <rest of query is the same>

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>
不一样的天空 2024-10-25 04:49:29

如果您要按以下方式排序: ORDER BY Parentid, id

那么您将得到您正在寻找的顺序,但它不会是有意的或任何东西,就像您的示例一样。

SQL 可能不是执行此类缩进组的最佳媒介。你可以......但最好在前端应用程序

编辑中完成:抱歉误读问题,Eric Petroelje 所说的。

编辑编辑:或者从表中选择,连接回自身(一个用于 Cat,一个用于 SubCat),然后指定不同的顺序,每个表一个。

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.

鸢与 2024-10-25 04:49:29

这:

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

返回:

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

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
无力看清 2024-10-25 04:49:29

你可以试试这个。它不完全是您指定的格式,但它符合您的要求吗?

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;

它产生以下内容:

+------+----------+----------+-------------+
| id   | name     | subcatid | subcatname  |
+------+----------+----------+-------------+
|    1 | CatOne   |        4 | SubCatOne   |
|    1 | CatOne   |        5 | SubCatOne2  |
|    2 | CatTwo   |     NULL | NULL        |
|    3 | CatThree |        6 | SubCatThree |
+------+----------+----------+-------------+

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 |
+------+----------+----------+-------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文