SQL Select 具有相同列的行的最后一行

SQL Select last row from row that have same column

我是 sql 的新手,经历了很多讨论和 youtube,但我变得更加困惑。

我正在使用 SELECT * FROM tp_posts 在我的网站中列出 post 的数量,所以基本上下面是我的 table 的样子。


          table name tp_posts
==========================================
|  ID       |       post   | tp_product
=================================================
|   1       |  toyota 1    | toyota
|   2       |  mercedez 1  | mercedez
|   3       |  bmw 1       | bmw
|   4       |  toyota 2    | toyota
|   5       |  toyota 3    | toyota
|   6       |  toyota 4    | toyota
==========================================

上面的 table 是它的外观示例,如您所见,如果我使用上面提到的普通查询,它将显示上面的所有项目,所以我开始使用这个

SELECT * FROM tp_posts GROUP BY tp_product


this works but it only shows the first toyota post and hide the latest ones and order by is not working also as group by hide the other posts

我想在过滤共享相同 tp_product 列的相关 post 时显示最新的 post,它必须显示上面的最后一辆丰田 post table


          table name tp_posts
========================================================
|  ID       |       post   | tp_product      post_numb
========================================================
|   1       |  toyota 1    | toyota        |   1
|   2       |  mercedez 1  | mercedez      |   1
|   3       |  bmw 1       | bmw           |   1
|   4       |  toyota 2    | toyota        |   2
|   5       |  toyota 3    | toyota        |   3
|   6       |  toyota 4    | toyota        |   4
==========================================

更新 这是完整的 table 现在你可以看到上面的 post 部分和 post_num 在 post 上相关 它显示丰田 1 tp_product这是产品类型 = toyota 和 post_numb = 1 表示 posts 部分中的 toyota 1。

有些人建议我使用 order by 但它并不像那样工作,因为我在 table 中得到了所有 post 无论如何我想要得到看起来像这样的结果 table 以下


table name tp_posts
========================================================
|  ID       |       post   | tp_product      post_numb
========================================================
|   6       |  toyota 4    | toyota        |   4
|   3       |  bmw 1       | bmw           |   1
|   2       |  mercedez 1  | mercedez      |   1

======================================================

我想在我的查询中得到上面的结果,如果我不够清楚请见谅谢谢

如果你总是想select最后一行,那么你可以使用下面的查询

SELECT * FROM tp_posts ORDER BY id DESC LIMIT 1;

否则分享上述数据的预期结果。然后只能建议解决方案。

我也是 SQL 的新手,但是 GROUP BY 语句通常与聚合函数(COUNT()、MAX()、MIN()、SUM()、AVG())一起使用,以按一列或多列对 result-set 进行分组。

您可以使用它,通过使用 MAX 到 select max post_numb 为子查询中的每个 tp_product,然后按降序对它们进行排序以实现您的期望的结果。

子查询将 select MAX post_numb 并且主查询将获取 post_numb 与子查询 select 根据 [=] 编辑的行14=].

SELECT * 
FROM tp_posts a
WHERE post_numb = (SELECT MAX(post_numb) 
                   FROM tp_posts b 
                   WHERE a.`tp_product` = b.`tp_product`)
GROUP BY tp_product 
ORDER BY id DESC

结果:

ID  post        tp_product  post_numb  
--  ----------  ----------  -----------
 6  toyota 4    toyota      4          
 3  bmw 1       bmw         1          
 2  mercedez 1  mercedez    1   

您可以使用 ROW_NUMBER() 函数生成按 tp_product 分区的行号,然后按 post_numb 降序排序;考虑到最大的 post_numb 将是“最新的”。将其作为子查询并仅对 return 被视为“最新”的查询执行 WHERE Rnum=1

SELECT ID, post, tp_product, post_numb
   FROM
(SELECT ID, post, tp_product, post_numb,
       ROW_NUMBER() OVER (PARTITION BY tp_product ORDER BY post_numb DESC) AS Rnum
    FROM tp_posts) v
    WHERE Rnum=1;

但是,这仅适用于 MySQL v8+ 和 MariaDB v10.2+。

Demo