簡體   English   中英

MySQL語句以最大ID獲得最小值

[英]MySQL statement to get min value with max ID

我想選擇具有最大ID的最小值。 例如,我想選擇Playstation,因為它的ID大於Silly 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  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

試試這個查詢。 這應該做。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM