MySQL语句获取具有最大ID的最小值
我想选择id最大的最小值。例如,我想选择Playstation,因为它的id比Dully Puddy大 这是我的sql语句:MySQL语句获取具有最大ID的最小值,mysql,sql,Mysql,Sql,我想选择id最大的最小值。例如,我想选择Playstation,因为它的id比Dully Puddy大 这是我的sql语句: SELECT *, max(id), min(price) FROM table group by type ORDER BY id DESC id name type price 123451 Park's Great Hits Music 19.99 123452 Sill
SELECT *, max(id), min(price)
FROM table
group by type
ORDER BY id DESC
id name type price
123451 Park's Great Hits Music 19.99
123452 Silly Puddy Toy 3.99
123453 Playstation Toy 3.99
我总是让傻乎乎的布迪回来买玩具。有什么不同的建议吗?
提前谢谢 只要id唯一-一个id只有一个价格,没有任何“最低”或“最高”,只有一个:
select * from table where id in (
SELECT max(id)
FROM table as a
where a.price = (select min(price) from table as b where a.type=b.type)
group by type
) as t
只要id唯一-一个id只有一个价格,没有任何“最低”或“最高”,只有一个:
select * from table where id in (
SELECT max(id)
FROM table as a
where a.price = (select min(price) from table as b where a.type=b.type)
group by type
) as t
尝试此查询。这应该可以做到
select t4.*
from
`table` t4
join
(
select t2.type, max(t2.id) as id
from
`table` t2 join
(
select type, min(price) as price
from
`table` t1
group by type
) t3 on t2.type = t3.type and t2.price = t3.price
group by t2.type
) t5 on t4.id = t5.id
尝试此查询。这应该可以做到
select t4.*
from
`table` t4
join
(
select t2.type, max(t2.id) as id
from
`table` t2 join
(
select type, min(price) as price
from
`table` t1
group by type
) t3 on t2.type = t3.type and t2.price = t3.price
group by t2.type
) t5 on t4.id = t5.id
你被搞糊涂了。既然
价格的组中,要求id
最大。这是总体上最大的id
。@MichaelBerkowski在这里你可以理解在价格最低的群体中成为最大的id
的要求。这是最伟大的id
整体。@MichaelBerkowski给你