简体   繁体   English

sql查询最新日期

[英]sql query by most recent date

I am listing item from a database, in an array of many database items. 我正在从数据库中列出许多数据库项目的数组中的项目。 I know what I am trying to do can be done by having separate table, however for this requirement, i wanted to see if it is possible to do the following. 我知道我要尝试执行的操作可以通过使用单独的表来完成,但是对于此要求,我想查看是否可以执行以下操作。

My code is: 我的代码是:

$sql_list  ="SELECT  *
    FROM ".$this->tables_rma."
    ORDER BY dateCreated DESC";

This returns an array of data in the table which is good, however I have multiple entries of the same item with different entities. 这将在表中返回一个数据数组,这很好,但是我具有相同条目的多个条目,但具有不同的实体。

So what i want to be able to do is select one of each entry, by its most recently updated row of data. 因此,我希望能够执行的操作是根据其最新更新的数据行选择每个条目之一。

Is this possible, or am i simply trying to cut corners? 这可能吗,还是我只是想偷工减料?

您可以使用SELECT DISTINCT [column]GROUP BY [column]

Something like this with LIMIT 1? LIMIT 1这样的东西?

$sql_list  ="SELECT  *
    FROM ".$this->tables_rma."
    ORDER BY dateCreated DESC LIMIT 1";

Suppose you have ID unique field and YourITEM field then: 假设您有ID唯一字段和YourITEM字段,然后:

select * from YourTable t1 where id = 
   (select id from YourTable t2 
     where t2.YourItem=t1.YourItem 
     order by DateCreated desc, id desc limit 1)

If you don't need last DateCreated value in your select then just: 如果您不需要选择中的最后一个DateCreated值,则只需:

select distinct YourItem from YourTable;

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

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