简体   繁体   English

在MySQL中通过代码获取带有min(price)组的行的值

[英]Getting the value of row with min(price) group by code in mysql

My table structure is like this: 我的表结构是这样的:

country                 | price  | code
---------------------------------------
EGYPT PROPER  WHOLESALE | 0.037  | 20
EGYPT FIXED             | 0.0710 | 20
EGYPT-OTHER             | .0497  | 20

I have to use GROUP BY code and I want the output as: 我必须使用GROUP BY代码,并且我希望输出为:

country                 | price  | code
---------------------------------------
EGYPT PROPER  WHOLESALE | 0.037  | 20

having the minimum value of price. 具有最低价格。

And my query is: 我的查询是:

select MIN(inp.price) as Price, inp.code, inp.country
from tbl_input_values as inp 
GROUP BY code

but i am getting the output as: 但我得到的输出为:

country         | price | code
------------------------------
EGYPT-OTHER     | .037  | 20

here, 这里,

SELECT  *
FROM    tbl_input_values
WHERE   price = (SELECT MIN(PRICE) FROM tbl_input_values)

This will show the rows with the minimum price, for every code: 对于每个代码,这将显示具有最低价格的行:

SELECT yourtable.*
FROM yourtable
WHERE (code, price) IN (select code, min(price)
                        from yourtable
                        group by code)

With multiple codes : 带有多个代码:

SELECT tb1.* 
FROM tbl_input_values tb1
INNER JOIN (select code, min(price) minPrice
            FROM tbl_input_values
            GROUP BY code) as a
   ON a.code = tb1.code and a.minPrice =tb1.price

I think the easiest way is: 我认为最简单的方法是:

select t.*
from t
order by price
limit 1

You don't need aggregation at all. 您根本不需要聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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