简体   繁体   English

(MySQL)OrderBy Field1 = 3,Field2

[英](MySQL) OrderBy Field1=3, Field2

I wish to order a table: 我想订购一张桌子:

Firstly by Field1=3 Then by Field2 DESC 首先按Field1 = 3,然后按Field2 DESC

I know I can't write OrderBy Field1=3, Field2 DESC 我知道我不能写OrderBy Field1 = 3,Field2 DESC

So how can I implement this?? 那么我该如何实现呢?

TO CLARIFY: 澄清:

Let's say I have a table of books. 假设我有一张桌子。 I wish to list ALL the books in the table. 我希望列出表中的所有书籍。 I wish the books from 1990 to appear at the top, then the rest of the books in alphabetical order of title. 我希望1990年以来的书排在顶部,然后其余书名按字母顺序排列。

Actually, you can write the statement you said you can't. 实际上,您可以写您说不能的声明。 Using your clarification example: 使用您的说明示例:

SELECT * FROM Books ORDER BY (year = 1990) DESC, name

"year = 1990" will be "1" for ones where year is 1990, so those will go at the top. 对于年份为1990的年份,“ year = 1990”将为“ 1”,因此它们将排在最前。

This is TSQL rather than MySQL, but it should give you the idea... 这是TSQL而不是MySQL,但它应该给您这个主意...

(Assuming I understand your question...) (假设我了解您的问题...)

ORDER BY
    CASE WHEN Field1 = 3 THEN 0 ELSE 1 END    ASC,
    Field2                                   DESC

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

相关问题 MYSQL仅在field1或field2不等于文本的情况下选择field1,field2 - MYSQL select field1, field2 only where field1 or field2 does not equal text MYSQL (field1, field2) = (x,y) vs field1= x AND field2 = Y 之间的差异 - MYSQL Difference between ( field1, field2 ) = (x,y) vs field1= x AND field2 = Y mysql相关子查询:选择field1,其中max(field2) - mysql correlated subquery : select field1 where max(field2) 复制field2时更新field1 - Update field1 when field2 is duplicated 参数化查询中的(?,?...?)或(@ field1,@ field2 ... @ fieldn)? - (?,?…?) or (@field1,@field2…@fieldn) in parmeterized queries? Mysql在哪里查询…选择* where field1 + field2 + field3 &lt;=字段4 - Mysql where query… select * where field1 +field2 + field3 <= field 4 Oracle查询,其中field1 = field2和field2 = field1 - Oracle query where field1=field2 and field2=field1 MySQL按多列组合排序(不按order1 asc,field2 asc排序) - MySQL Order by multiple column combined (not order by field1 asc, field2 asc) 在 Hive 中,if(field1=field2=field3=0,'0','1') 未给出预期结果 - In Hive, if(field1=field2=field3=0,'0','1') is not giving expected results SQL(DQL):WHERE值IN(数组field1)或field2 =值 - SQL (DQL): WHERE value IN (array field1) OR field2 = value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM