mysql - 尝试限制 mysql 结果中显示的数量

标签 mysql sql

我有一个表,用于存储 ID、商品、食物类型、图像和价格

我想将结果限制为商店中出售的每种类型之一,例如狗粮、猫粮、DVD 按降序排列 mysql 数据库中保存的内容示例 表:Nunca

id      item                    type        image   price
=========================================================
1       Pedigree Pouches        dog food    img1    $4.99
2       Cesar Classic Selection dog food    img2    $3.99
3       Meaty Strips Chicken    dog food    img3    $2.99
4       Jelly Fish Selection    cat food    img4    $1.99
5       Bananas                 fruit       img5    $2.84
6       Celery                  vegetables  img6    $5.99
7       Apple                   fruit       img7    $0.95
8       Pineapple Juice         Soft drink  img8    $0.99
9       Oranges                 fruit       img9    $1.99
10      Cabbage                 vegetables  img10   $0.99
11      Suicide Squad           dvd         img11   $12.99

查询的最终结果应如下所示

id      item                    type        image   price
==========================================================
11      Suicide Squad           dvd         img11   $12.99
10      Cabbage                 vegetables  img10   $0.99
9       Oranges                 fruit       img9    $1.99
8       Pineapple Juice         Soft drink  img8    $0.99
4       Jelly Fish Selection    cat food    img4    $1.99
3       Meaty Strips Chicken    dog food    img3    $2.99

我尝试了各种mysql查询,但无济于事

最佳答案

select * from t where 
id in(select max(t.id) id from t group by t.type) 
order by id desc

我猜可能有窗口或连接替代方案,但这个似乎适合此目的。

关于mysql - 尝试限制 mysql 结果中显示的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321234/

相关文章:

php - 错误SQL用php创建临时表

php - jQuery UI 自动完成 - 未找到结果

c# - Linq To 实体查询返回错误数据(不同于 Management Studio 查询)

mysql - 如何减少sql查询的加载时间?

mysql - 如何更正以下查询

sql - 如何在 BigQuery 中显示数据样本?

sql - 使用 > 比较列时出错

sql - 查询不使用索引 --

mysql - 如何显示我们在两个表的内连接中获得的行中的列差异

mysql - 如何从当前系统日期获取月份