简体   繁体   English

我需要使用 php - mysql 列出产品的唯一最新版本

[英]I need to list the only latest versions of the products with php - mysql

I'm just trying to list the only latest versions of the products.我只是想列出产品的唯一最新版本。 Not all versions with the same product name.并非所有版本都具有相同的产品名称。

So this:所以这: 在此处输入图片说明

Instead of this:取而代之的是: 在此处输入图片说明

* tables are not so important I just want the code to get the data. *表不是那么重要,我只想要代码来获取数据。
* tables are not so important I just want the code to get the data. *表不是那么重要,我只想要代码来获取数据。

This is my code but lists nothing :这是我的代码,但没有列出任何内容:

include("databaseinfo.php");
$sql = "SELECT product_name,release_date,version FROM product GROUP BY product_name ORDER BY product_ID DESC";
$result = mysqli_query($conn,$sql);
while ($b=mysqli_fetch_array($result,MYSQLI_ASSOC)){
    echo $b['product_name']." ".$b['release_date']." ".$b['version'];
}

My product table:我的产品表:

在此处输入图片说明

One option is to filter with a subquery:一种选择是使用子查询进行过滤:

select product_name, release_date, version
from product p
where release_date = (
    select max(p1.release_date)
    from product p1
    where p1.product_id = p.product_id
)

If you are running MySQL 8.0, you can also use rank() :如果您运行的是 MySQL 8.0,您还可以使用rank()

select product_name, release_date, version
from (
    select p.*, rank() over(partition by product_id order by release_date desc)
    from product
) t
where rn = 1

This assumes that product_id uniquely identifies each product (so your table would have several rows per product_id ) - if that's not the case, use product_name instead.这假设product_id唯一标识每个产品(因此您的表每个product_id会有几行) - 如果不是这种情况,请改用product_name

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

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