简体   繁体   English

mySQL 如何获取按另一列分组并按日期排序的 COUNT 中可见的最后日期

[英]mySQL how to get last date visible in a COUNT grouped by another column and ordered by date

I have a table with columns these columns:我有一个包含这些列的表:

date日期
brand
order_number订单号

I need to show a report grouped by brand, ordered by date descending, counted by order_number, showing the most recent date of the last order for each brand.我需要显示按品牌分组的报告,按日期降序排列,按 order_number 计数,显示每个品牌最后一个订单的最近日期。

My code is like this:我的代码是这样的:

<?php
$sql = "SELECT *, COUNT(*) FROM table GROUP BY brand order by date DESC;";
$resultg = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $date = $row["date"];
    $brand = $row["brand"];
    echo "<table>";
        echo "<tr>";  
            echo "<td class='timecell'>";
                echo date('d-m H:i.s', strtotime($date));
            echo "</td>";
            echo "<td class='brandcell'>";
                echo $brand;
            echo "</td>";
            echo "<td class='countcell'>";
                echo $row['COUNT(*)'];
            echo "</td>";
        echo "</tr>";
    echo "</table>";
}
?>

The output I get is ordered by date descending and it shows how many orders for each brand, but the date showed for each group is the date of the first order for that brand, and also the order in the list is made using the date of the first order for that brand, while I need to show the date of the last order for that brand and have the list ordered descending using the date of the last order for that brand.我得到的 output 是按日期降序排列的,它显示了每个品牌的订单数量,但每个组显示的日期是该品牌的第一个订单日期,并且列表中的订单也是使用日期该品牌的第一个订单,而我需要显示该品牌的最后一个订单的日期,并使用该品牌的最后一个订单的日期降序排列列表。 How must be written the query to get that result?必须如何编写查询才能获得该结果?

Maybe using a MAX(date) together with the COUNT(*) if I understood correctly?如果我理解正确,也许将 MAX(date) 与 COUNT(*) 一起使用?

This query would show all orders sorted by date and how many orders of the brand you got.此查询将显示按日期排序的所有订单以及您获得的品牌订单数量。

SELECT t1.*, count_b 
FROM table1 t1 
    INNER JOIN (SELECT brand, COUNT(*) count_b FROM table1 GROUP BY brand) b ON t1.brand = b.brand 
 order by date DESC

Found the solution.找到了解决方案。

The goal was to group the orders by brand, to show the date of the last order for that brand and to have the list ordered descending by the date of the last order for each brand.目标是按品牌对订单进行分组,显示该品牌的最后一个订单日期,并使列表按每个品牌的最后一个订单日期降序排列。

The sql syntax to get the goal is:达到目标的 sql 语法是:

SELECT *, MAX(date), COUNT(*) FROM table GROUP BY brand order by MAX(date) DESC;

This produce the correct result and can be formatted like follows:这会产生正确的结果,并且可以格式化如下:

<?php

$sql = "SELECT *, COUNT(*) FROM table GROUP BY brand order by date DESC;";
$resultg = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $brand = $row["brand"];
    echo "<table>";
        echo "<tr>";  
            echo "<td class='timecell'>";
                $row['MAX(date)'];
            echo "</td>";
            echo "<td class='brandcell'>";
                echo $brand;
            echo "</td>";
            echo "<td class='countcell'>";
                echo $row['COUNT(*)'];
            echo "</td>";
        echo "</tr>";
    echo "</table>";
}

?>

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

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