简体   繁体   English

如何使用按字段排序以及按ID排序

[英]how to use order by field as well sort by id

i have the lot of data and i have to short data that has on Approval and Approval data with short id descending. 我拥有大量数据,并且我必须对具有批准号的数据和具有短ID降序的批准数据做空。

Like the following query: 类似于以下查询:

 $query = "select * from event ORDER BY FIELD(status, 'Approval') desc";

But when new data added in event table. 但是当新数据添加到事件表中时。 as when new data added in table its status is on Approval. 当新数据添加到表中时,其状态为“批准”。 and when i show the data .data is not sorted with event id descending. 当我显示数据时,数据未按事件ID降序排序。

I am confused how to use both order by field and order by id descending please help 我很困惑如何同时使用按字段排序和按ID降序排序,请帮助

Thanks and Regards 谢谢并恭祝安康

You can do as following if you want the Approval to appear first and then by descending order of id 如果要首先显示批准,然后按ID降序排列,可以执行以下操作

select * from event 
ORDER BY FIELD(status, 'Approval') desc ,event_id desc

Here is a real time example 这是一个实时示例

mysql> select idusers from users where status = 'paid' order by idusers desc limit 1 ;
+---------+
| idusers |
+---------+
|  150949 |
+---------+
1 row in set (0.00 sec)

Above is to get the most recent paid user 以上是获得最近付费用户的信息

mysql> select idusers,status from users order by field (status,'paid')desc , 
        idusers desc limit 3;
+---------+--------+
| idusers | status |
+---------+--------+
|  150949 | paid   |
|  150948 | paid   |
|  150947 | paid   |
+---------+--------+

In the above the most recent is the first row followed by others. 在上面的最新行是第一行,然后是其他行。

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

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