简体   繁体   English

如何使用PHP按照降序从日期中筛选表中的数据

[英]How to filter data from table with date in descending order using PHP

I need to fetch all data from table as per date field in descending order using PHP and Mysql. 我需要使用PHP和Mysql按照日期字段的降序从表中获取所有数据。 I am explaining my table below. 我正在解释下面的表格。

db_special: db_special:

id          name         date

1            Ram          12/04/2017 15:31:57 PM

2            Raj          12/03/2017 05:31:57 AM

3            Rahul        11/28/2017 12:30:54 PM

Here is my query: 这是我的查询:

select * 
from db_special 
where
    STR_TO_DATE(date, '%m/%d/%Y') 
    BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW()

Here I implemented one logic to fetch last 7 days data and now I need to implement sorting as per the same date field in descending order using MySQL and PHP . 在这里,我实现了一种逻辑来获取最近7天的数据,现在我需要使用MySQL and PHP根据同一日期字段以降序实现排序。

Are you really using a VARCHAR/CHAR/CLOB field to store a date? 您是否真的使用VARCHAR / CHAR / CLOB字段存储日期? That is very bad. 那是非常糟糕的。 It makes kittens cry. 它使小猫哭泣。 Please don't do it. 请不要这样做。

using PHP and Mysql 使用PHP和Mysql

No. you don't retrieve data from a DBMS then sort it in PHP. 不。您不从DBMS检索数据,而是用PHP对其进行排序。 That too is very bad (unless you're Facebook, in which case it is possibly a bit silly, but that is another story). 那也非常糟糕(除非您是Facebook,在这种情况下可能有点愚蠢,但这是另一回事了)。 You sort in the database. 您在数据库中排序。

Just add an ORDER BY clause at the end of your SELECT 只需在SELECT的末尾添加一个ORDER BY子句

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

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