Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图限制mysql结果中显示的数量_Mysql_Sql - Fatal编程技术网

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

试图限制mysql结果中显示的数量,mysql,sql,Mysql,Sql,我有一张桌子,上面有id、物品、食物类型、图片和价格 我想将结果限制在商店出售的每种类型中的一种,例如狗粮、猫粮、dvd,按降序排列 mysql数据库中保存的内容示例 表:Nunca id item type image price ========================================================= 1 Pedigree Pouches dog food

我有一张桌子,上面有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 n.*
from nunca n
where n.id = (select max(n2.id) from nunca n2 where n2.type = n.type);

这将选择每种类型的最后一行。

典型方法使用相关子查询:

select n.*
from nunca n
where n.id = (select max(n2.id) from nunca n2 where n2.type = n.type);
select * from t where 
id in(select max(t.id) id from t group by t.type) 
order by id desc
这将选择每种类型的最后一行

select * from t where 
id in(select max(t.id) id from t group by t.type) 
order by id desc
我想可能会有窗口或连接的选择,但这一个似乎适合这个目的


我想可能会有窗口或连接选项,但这一个似乎适合此用途。

这只生成一个记录,即id项目类型图像价格11自杀队dvd img11$12。99@Mr.Bean:可能您没有复制where t2.type=t.type零件?至少这可以解释您的一行结果。我复制了它并输入了正确的详细信息,但它只显示了一行。这是我使用的Select Nunca。*from Nunca where Nunca.id=Select maxt2.id from Nunca t2 where Nunca.type=Nunca.type@MrBean:用t2替换Nunca.type=Nunca.type中两个记录中的一个。这只生成一条记录,即id项目类型图像价格11自杀队dvd img11$12。99@Mr.Bean:可能您没有复制where t2.type=t.type零件?至少这可以解释您的一行结果。我复制了它并输入了正确的详细信息,但它只显示了一行。这是我使用的Select Nunca。*from Nunca where Nunca.id=Select maxt2.id from Nunca t2 where Nunca.type=Nunca.type@Bean先生:将Nunca.type=Nunca.type中的两个Nunca中的一个替换为t2。非常感谢,这正好击中了头部。非常感谢,这正好击中了头部。非常感谢