gpt4 book ai didi

MySql:按特定条件对 2 列进行排序

转载 作者:行者123 更新时间:2023-11-30 22:03:33 25 4
gpt4 key购买 nike

我有一个包含以下列的社区表,如下所示:

enter image description here

我需要按“ftr_order”和“isFeatured”列对表进行排序,以便按以下顺序显示数据:

ftr_order | isFeatured

2 | yes
1 | no
2 | no
3 | no
4 | no
5 | no
6 | no
0 | no
NULL | no
NULL | no
NULL | no

ftr_order 应按升序对数据进行排序。 0 和 NULL 值应位于表的底部。我尝试了以下查询,这在一定程度上解决了它,但我需要设置优先级,即

i) 第一优先级:如果 isFeatured,则将其显示在顶部。

ii) Second Priority:按升序对ftr_order排序

** 注意:如果ftr_order0NULL则在底部显示数据。

SELECT * FROM community ORDER BY if(ftr_order = 0 or ftr_order is null,1,0), ftr_order ASC, community_description ASC

这将显示以下输出:

enter image description here

在这里你可以看到 ftr_order 已经被正确排序但是这里缺少一件事,ID 编号 7 应该列在表格的顶部作为这一行有列 isFeatured='yes'。为了将这一行置于顶部,我尝试通过写下查询来解决它:

SELECT * FROM community ORDER BY if(ftr_order = 0 or ftr_order is null,1,0 or isFeatured='yes'), ftr_order ASC, community_description ASC

可以看到生成了如下输出,还可以分析还剩下什么问题。 ID 7 号应该列在表格的顶部,但这里ID 7 号列在表格的底部。因此我需要将它移到顶部。请帮我解决这个问题。提前致谢!

enter image description here

最佳答案

SELECT * FROM community ORDER BY isfeatured desc, ftr_order ASC, community_description ASC

如果 isfeatured 只有 yes/no make it simple,你可以这样尝试

关于MySql:按特定条件对 2 列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com