简体   繁体   English

Mysql PHP查询选择

[英]Mysql PHP Query select

I want to order desc some rows from mysql table by a numberic value . 我想按数字值从mysql表中排序desc一些行。

Such as: SELECT * FROM table ORDER BY ABS(row1) DESC . 如: SELECT * FROM table ORDER BY ABS(row1) DESC

Table format: 表格格式:

ID | ID | Type | 类型 Value

1 | 1 | 2 | 2 | 42 42

2 | 2 | 1 | 1 | 52 52

3 | 3 | 2 | 2 | 67 67

4 | 4 | 2 | 2 | 50 50

5 | 5 | 1 | 1 | 15 15

6 | 6 | 1 | 1 | 27 27

7 | 7 | 3 | 3 | 48 48

8 | 8 | 3 | 3 | 23 23

And i want to get the following result - to sort by value DESC but first show rows with type 2 and DESC and after that the type 1,3,4 (...) rows DESC: 我想得到以下结果-按值DESC排序,但首先显示类型2和DESC的行,然后显示类型1,3,4(...)的行DESC:

ID | ID | Type | 类型 Value

3 | 3 | 2 | 2 | 67 67

4 | 4 | 2 | 2 | 50 50

1 | 1 | 2 | 2 | 42 42

2 | 2 | 1 | 1 | 52 52

7 | 7 | 3 | 3 | 48 48

6 | 6 | 1 | 1 | 27 27

9 | 9 | 3 | 3 | 23 23

5 | 5 | 1 | 1 | 15 15

您可以按两个或多个字段排序

SELECT * FROM table ORDER BY ABS(row1), row2, row3 DESC

This is the order by you want: 这是您想要的order by

order by `type` = 2 desc,
         value desc

The expression type = 2 returns a boolean value, which is equivalent to 1 when true and 0 when false. 表达式type = 2将返回一个布尔值,该值等于true时为1,为false时为0。 Your example doesn't have negative values, so I don't see a need for abs() . 您的示例没有负值,因此我认为不需要abs()

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

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