是否可以根据条件以两种不同的方式对 SELECT 查询的结果进行排序?

发布于 2024-10-14 10:59:19 字数 603 浏览 12 评论 0原文

以下是无效语法,但本质上是我想说的:

  SELECT *  
    FROM categories 
ORDER BY menu_order ASC WHERE parent = '1' || IF parent = '0' ORDER BY id ASC;

是否可以根据查询中的条件对选择查询的结果进行不同的排序?

以防万一我不清楚,我正在尝试按菜单顺序对具有父级的类别进行排序,而对没有父级的类别则按 id 进行排序。

编辑

所以数据库如下所示:

id | name    | parent | menu_order

==================================

1  | Apparel | 0      | 2
2  | Sports  | 0      | 4
3  | Hats    | 1      | 5
4  | Weights | 1      | 3
5  | Lights  | 0      | 1

查询的结果集应该是:

Apparel
Sports
Lights

Weights
Hats

The following is invalid syntax but it is what I'm trying to say essentially:

  SELECT *  
    FROM categories 
ORDER BY menu_order ASC WHERE parent = '1' || IF parent = '0' ORDER BY id ASC;

Is it possible to order the results of a select query differently based on a conditional within the query?

Just in case I wasn't clear, I am trying to order the categories with a parent by menu order and those without a parent by id.

EDIT

So here's how the database looks like:

id | name    | parent | menu_order

==================================

1  | Apparel | 0      | 2
2  | Sports  | 0      | 4
3  | Hats    | 1      | 5
4  | Weights | 1      | 3
5  | Lights  | 0      | 1

The result set of the query should be:

Apparel
Sports
Lights

Weights
Hats

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

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

发布评论

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

评论(4

慢慢从新开始 2024-10-21 10:59:19

order by 中的用例,如下所示:

SELECT * FROM categories
ORDER BY
CASE WHEN parent = '1'
    THEN menu_order
    ELSE id
END;

use case in the order by, like this:

SELECT * FROM categories
ORDER BY
CASE WHEN parent = '1'
    THEN menu_order
    ELSE id
END;
世态炎凉 2024-10-21 10:59:19

如果您将数据更改为

insert categories values (1 , 'Apparel ', 0 , 1)
insert categories values (2 , 'Sports  ', 0 , 5)
insert categories values (3 , 'Hats    ', 1 , 3)
insert categories values (4 , 'Weights ', 1 , 4)
insert categories values (5 , 'Lights  ', 0 , 2)

“使用可接受的解决方案的输出”,则结果为“

1   Apparel     0   1
2   Sports      0   5
3   Hats        1   3
4   Weights     1   4
5   Lights      0   2

这就是您所追求的吗?”杰拉特想问的是,这就是你所追求的吗?

1   Apparel     0   1
2   Sports      0   5
5   Lights      0   2
-------------------------------- << line for emphasis
3   Hats        1   3
4   Weights     1   4

即所有无父级的,在所有有父级的之前,这是由下面的查询生成的

SELECT *
FROM categories
ORDER BY parent,
    CASE
    WHEN parent = '1'
    THEN menu_order
    ELSE id
END;

If you changed your data to

insert categories values (1 , 'Apparel ', 0 , 1)
insert categories values (2 , 'Sports  ', 0 , 5)
insert categories values (3 , 'Hats    ', 1 , 3)
insert categories values (4 , 'Weights ', 1 , 4)
insert categories values (5 , 'Lights  ', 0 , 2)

The output using the accepted solution comes out as

1   Apparel     0   1
2   Sports      0   5
3   Hats        1   3
4   Weights     1   4
5   Lights      0   2

Is that what you are after? What Gerrat is trying to ask is, is this what you are after?

1   Apparel     0   1
2   Sports      0   5
5   Lights      0   2
-------------------------------- << line for emphasis
3   Hats        1   3
4   Weights     1   4

i.e. all the unparented ones, before all the parented ones, which is produced by the query below

SELECT *
FROM categories
ORDER BY parent,
    CASE
    WHEN parent = '1'
    THEN menu_order
    ELSE id
END;
江南月 2024-10-21 10:59:19

我处理此类功能请求的方式是按需构建查询的该部分。我不确定你的程序逻辑是什么样的,所以很难说具体。例如,使用 ibatis,人们可以根据参数选择要包含哪个子句。

The way I've handled this type of feature request is to have that part of the query built on demand. I'm not sure what your program logic looks like, so it's hard to say specifically. Using ibatis, for example, one could choose which clause to include based upon a parameter.

欢你一世 2024-10-21 10:59:19

您的 select 子句可以定义自定义“order_by”字段,您可以按该字段进行排序。

Your select clause could define a custom 'order_by' field, and you could order by that.

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