SQL从具有同一列的行选择最后一行

发布于 2025-02-02 01:32:01 字数 1956 浏览 3 评论 0原文

我是SQL的新手,经历了很多讨论和YouTube,但我感到更加困惑。

我正在使用选择 *从TP_POSTS中列出帖子数量到我的网站的数字,因此基本上是我的表格。


          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
==========================================

上表是外观的示例,您可以看到,如果我使用上面提到的普通查询,它将显示上述所有i

SELECT * FROM tp_posts GROUP BY tp_product


项最新的和订单也无法通过隐藏其他帖子作为群体工作

我想显示最新帖子,而在过滤相关的帖子共享相同的tp_product列,它必须从上表


          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
==========================================

更新 中显示最后一篇丰田帖子现在,这是完整的表,如上所述,POST_NUM与帖子相关,它显示了Toyota 1 TP_Product,它是产品类型= Toyota和post_numb = 1,在帖子部分中代表Toyota 1。

有些人建议我使用订单,但它不起作用,因为我在表格中获得所有帖子,无论哪种方式,我都希望得到下面的表现,


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

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

我想在我的查询中获得上述结果,对不起,如果我还不够清楚谢谢

I am new to sql and went through alot of discussion and youtube but i get more confused.

I was using SELECT * FROM tp_posts to list number of posts into my website so basically below is how my table looks like.


          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
==========================================

Above table is example on how it looks as you can see it will show all the i items above if i use the normal query i mentioned above so i started using this

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

I want to display the latest post while filtering related posts that share the same tp_product column, it has to show the last toyota post from above 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
==========================================

Update This is the complete table now as you can see above post section and post_num are related on post it shows toyota 1 tp_product which is product type = toyota and post_numb = 1 which represents toyota 1 in posts section.

Some people suggested i use order by but it doesn't work like that as i get all posts in table either way i want to get the result that look like this table below


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

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

I want to get The above results in my query, sorry if i was not clear enough Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月隐月明月朦胧 2025-02-09 01:32:01

您可以使用row_number()函数来生成由tp_product划分的行号,然后通过post_numb discending desceend;考虑到最大的post_numb将是“最新”。将其作为子查询并执行,其中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+一起使用。

You can use ROW_NUMBER() function to generate row number partitioned by tp_product then order by post_numb descending; with consideration that the largest post_numb will be the "latest". Make that as subquery and do a WHERE Rnum=1 to only return the ones being deemed as "latest":

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;

However, this only work with MySQL v8+ and MariaDB v10.2+.

Demo

百合的盛世恋 2025-02-09 01:32:01

如果您始终想选择最后一行,则可以使用以下查询,

SELECT * FROM tp_posts ORDER BY id DESC LIMIT 1;

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

if you always want to select last row then you can use below query

SELECT * FROM tp_posts ORDER BY id DESC LIMIT 1;

Else share the expected result from above data. Then only solution can be suggested.

逆光下的微笑 2025-02-09 01:32:01

您可以通过使用max来选择max post_numb在每个tp_product内部的中,然后按下降顺序对其进行排序以实现您期望的结果。

子查询将选择max post_numb,主查询将获取post_numb匹配根据tp_product选择的行。

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   

You can use this, by using MAX to select the max post_numb for each tp_product inside a subquery, then sorting them in a descending order to achieve your desired result.

The subquery will select the MAX post_numb and the main query will get the row where the post_numb matches what was selected by the subquery according to tp_product.

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

Result:

ID  post        tp_product  post_numb  
--  ----------  ----------  -----------
 6  toyota 4    toyota      4          
 3  bmw 1       bmw         1          
 2  mercedez 1  mercedez    1   
最丧也最甜 2025-02-09 01:32:01

我也是SQL的新手,但是
该组按语句通常与聚合函数(count(),max(),min(),sum(),avg())一起使用,以通过一个或多个列对结果集进行分组。

I am new to SQL too, but
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文