简体   繁体   English

MySql:按特定条件排序2列

[英]MySql: Sort 2 columns by certain conditions

I have community table with following columns as shown below: 我有以下列的社区表,如下所示:

在此输入图像描述

I need to sort table by " ftr_order " and " isFeatured " columns in a certain way that it displays data in the following order: 我需要按照“ 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 should sort data in ascending order. ftr_order应按升序对数据进行排序。 0 and NULL values should come at the bottom of the table. 0和NULL值应该位于表的底部。 I have tried the following query, this resolved it to some extent, but I need to set priority ie 我尝试了以下查询,这在一定程度上解决了它,但我需要设置优先级即

i) First Priority : If isFeatured is yes then show it on the top. i) 第一优先级 :如果isFeatured 是,则将其显示在顶部。

ii) Second Priority : Sort ftr_order in ascending order ii) 第二优先级 :按升序排序ftr_order

** Note: If ftr_order is 0 or NULL then display data at bottom. ** 注意:如果ftr_order0NULL,则在底部显示数据。

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

This displays the following output: 这将显示以下输出:

在此输入图像描述

Here you can see that ftr_order has been sorted out correctly but one thing is missing here, ID number 7 should be listed on the top of the table as this row has column isFeatured ='yes'. 在这里你可以看到ftr_order已经正确排序但是这里缺少一件事, ID号7应该列在表的顶部,因为这行有列isFeatured ='yes'。 In order to bring this row on the top I tried to resolve it by writing down the query: 为了将此行置于顶部,我尝试通过写下查询来解决此问题:

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

You can see the following output is generated and you can also analyze what issue is still left. 您可以看到生成了以下输出,您还可以分析仍然存在的问题。 ID number 7 should be listed on the top of the table, but here ID number 7 is listed at the bottom of the table. ID号7应该列在表的顶部,但是这里的ID号7列在表的底部。 Therefore I need to shift it on the top. 因此我需要将它移到顶部。 Please help me out to resolve this issue. 请帮我解决这个问题。 Thanks in advance! 提前致谢!

在此输入图像描述

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

You can try like this if isfeatured has only yes/no make it simple 你可以尝试这样,如果isfeatured只有是/否使它变得简单

Finally I got answer to my own question, solution is as follows: 最后我得到了回答我自己的问题,解决方案如下:

SELECT * from (SELECT * FROM `community` ORDER BY if( ftr_order =0 OR ftr_order IS NULL , 1, 0 ) , ftr_order ASC , community_description ASC) as community ORDER BY CASE WHEN isFeatured ='yes' then isFeatured END desc

Following output will be generated: 以下输出将生成:

在此输入图像描述

Here isFeatured ='yes' is at the top and rest of the data is order by ftr_order ascending where NULL and 0 will come at the end. 这里isFeatured ='yes'位于顶部,其余数据按ftr_order升序排序,其中NULL和0将在结尾处。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM