简体   繁体   中英

get id where price is max in mysql

My table is:

id  code    batch price    qty
---|----- |----- |-----|---------
1  | 107  | 1    | 39  | 399 
2  | 107  | 1    | 39  | 244
3  | 107  | 2    | 40  | 555
4  | 108  | 1    | 70  | 300
5  | 108  | 2    | 60  | 200
6  | 109  | 2    | 50  | 500
7  | 109  | 2    | 50  | 600
8  | 110  | 2    | 75  | 700

My desired result is (i want this result as output)

id  code    batch price  
---|----- |----- |-----|
3  | 107  | 2    | 40  |
4  | 108  | 1    | 70  |
3  | 109  | 2    | 50  | 
8  | 110  | 2    | 75  | 

i write this query

SELECT `id`,`code`,`batch` max(`price`) FROM `table_name` where `qty` > 0  group by `code`

my output is

id  code    batch price  
---|----- |----- |-----|
1  | 107  | 1    | 40  |
4  | 108  | 1    | 70  |
6  | 109  | 2    | 50  | 
8  | 110  | 2    | 75  | 

i need id and batch where the price is max

Another way to get highest record per group

select *
from demo a
where (
  select count(*)
  from demo b
  where a.code = b.code
  and case when a.price = b.price then a.id < b.id else a.price < b.price end
) = 0

I assume id is auto increment so in case there is a tie in max price from groups you can use a CASE to pick the latest id

Demo

You could use join on the max value grouped by code

select  a.id, a.code, a.batch, b.max_price
from table_name a 
inner join  (
  select code, max(price) as max_price
  from table_name
  group by code 
) b on a.code = b.code  and a.price = b.max_price

and if you have more that a row with same code, price you could use

select  max(a.id), a.code, a.batch, b.max_price
from table_name a 
inner join  (
  select code, max(price) as max_price
  from table_name
  group by code 
) b on a.code = b.code  and a.price = b.max_price   
group by a.code, a.batch, b.max_price

Order by the price and then limit the result to 1 :)

SELECT id, batch 
FROM table_name
ORDER BY price DESC 
LIMIT 1

Give a row number group by code and in the descending order of price columns. Then select the rows having row number 1.

Query

select t1.`id`, t1.`code`, t1.`batch`, t1.`price` from (
    select `id`, `code`, `batch`, `price`, (
        case `code` when @curA 
        then @curRow := @curRow + 1 
        else @curRow := 1 and @curA := `code` end 
    ) as `rn`
    from `MyTable` t, 
    (select @curRow := 0, @curA := '') r 
    order by `code`, `price` desc 
)t1 
where t1.`rn` = 1
order by `code`;

Find a demo here

select id,batch from table_name order by price desc limit 0,1
  • Sort by desc with price
  • Select the top row using limit

Try this:

SELECT `id`,`code`,`batch`, `price`
FROM `table_name`
WHERE `qty` > 0
GROUP BY `code` HAVING `price` =  max(`price`)

try this
SELECT MAX( price ) AS price, id , batch FROM table_name

I didn't understand you need single result or multiple. but this works fine if you need only max id.

 SELECT id 
    FROM table 
    WHERE id=(
        SELECT max(price) FROM table
        )

Note : if the value of max(id) is not unique, multiple rows are returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM