Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按日期排序,仅包含每个产品的最新行_Php_Mysql - Fatal编程技术网

Php 按日期排序,仅包含每个产品的最新行

Php 按日期排序,仅包含每个产品的最新行,php,mysql,Php,Mysql,要创建表,我将使用以下查询检索数据: SELECT cId,cProductId,cDate,cInfo FROM tProduct ORDER BY cProductId ASC,cDate ASC; 很自然,我简化了很多查询,但您得到的结果是,同一个产品可能有许多记录,而且是唯一一条我感兴趣的最新记录,因此我如何排序查询:我只存储while循环中遇到的最后一行的信息 while (($row = $result->fetch()) !== FALSE) { if(!emp

要创建表,我将使用以下查询检索数据:

SELECT cId,cProductId,cDate,cInfo FROM tProduct ORDER BY cProductId ASC,cDate ASC;
很自然,我简化了很多查询,但您得到的结果是,同一个产品可能有许多记录,而且是唯一一条我感兴趣的最新记录,因此我如何排序查询:我只存储while循环中遇到的最后一行的信息

while (($row = $result->fetch()) !== FALSE)
{
      if(!empty($row[0])){
          if(($table_row[1]!=$row[1])&&(!empty($table_row[0]))){
              //Display the last row saved
          }
          $table_row= array($row[0],$row[1],$row[2],$row[3]);
      }
}

现在我想为生成的html表实现一些排序选项。我将把排序信息存储在会话变量中。在排序选项中,我希望能够按日期排序。在按日期对查询进行排序时,如何确保仅检索每个产品的最新行?

您的查询中没有包含
FROM
子句。但是,假设您需要每个产品ID的最新行,可以执行以下操作:

SELECT cId,cProductId,cDate,cInfo
FROM tProduct t1
WHERE cDate = (SELECT max(cDate) FROM tProduct t2 where t1.cProductID = t2.cProductId)
ORDER BY cProductId ASC,cDate ASC;