当前位置: 动力学知识库 > 问答 > 编程问答 >

php - 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?

网友答案:

Suppose you have ID unique field and YourITEM field then:

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:

select distinct YourItem from YourTable;
网友答案:

You can use SELECT DISTINCT [column] or GROUP BY [column]

网友答案:

Something like this with LIMIT 1?

$sql_list  ="SELECT  *
    FROM ".$this->tables_rma."
    ORDER BY dateCreated DESC LIMIT 1";
分享给朋友:
您可能感兴趣的文章:
随机阅读: