SQL从具有同一列的行选择最后一行
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
row_number()
函数来生成由tp_product
划分的行号,然后通过post_numb
discending desceend;考虑到最大的post_numb
将是“最新”。将其作为子查询并执行,其中rnum = 1
仅返回被视为“最新”的:但是,这仅与mySQL v8+和mariadb v10.2+一起使用。
You can use
ROW_NUMBER()
function to generate row number partitioned bytp_product
then order bypost_numb
descending; with consideration that the largestpost_numb
will be the "latest". Make that as subquery and do aWHERE Rnum=1
to only return the ones being deemed as "latest":However, this only work with MySQL v8+ and MariaDB v10.2+.
Demo
如果您始终想选择最后一行,则可以使用以下查询,
否则共享上述数据的预期结果。然后只能建议解决方案。
if you always want to select last row then you can use below query
Else share the expected result from above data. Then only solution can be suggested.
您可以通过使用
max
来选择max post_numb
在每个tp_product
内部的中,然后按下降顺序对其进行排序以实现您期望的结果。子查询将选择
max post_numb
,主查询将获取post_numb
匹配根据tp_product
选择的行。结果:
You can use this, by using
MAX
to select themax post_numb
for eachtp_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 thepost_numb
matches what was selected by the subquery according totp_product
.Result:
我也是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.