使用mysql中的代码获取min(price)组的行值

时间:2013-01-28 15:06:08

标签: mysql sql

我的表结构是这样的:

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

我必须使用GROUP BY代码,我希望输出为:

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

具有最低价格价值。

我的疑问是:

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

但我得到的输出为:

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

4 个答案:

答案 0 :(得分:2)

这里,

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

答案 1 :(得分:1)

这将显示每个代码的最低价格行:

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

答案 2 :(得分:1)

使用多个代码:

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

答案 3 :(得分:0)

我认为最简单的方法是:

select t.*
from t
order by price
limit 1

根本不需要聚合。