Mysql 使用聚合显示不同的值

Mysql 使用聚合显示不同的值,mysql,sql,distinct,aggregate-functions,min,Mysql,Sql,Distinct,Aggregate Functions,Min,我有一个记录不同供应商每日价格的表格。我的目标是找到最好的(低价)供应商。 表结构是 表名:lab1 列:ID、产品ID、价格日期、价格、供应商 ----------------------------------------------------------------------------------- ID Product_ID Price_date Price Supplier ------------------------------------------------

我有一个记录不同供应商每日价格的表格。我的目标是找到最好的(低价)供应商。 表结构是 表名:lab1 列:ID、产品ID、价格日期、价格、供应商

-----------------------------------------------------------------------------------
ID  Product_ID  Price_date  Price   Supplier
--------------------------------------------------------------------------------------
1   8           26-10-2014  1300    SP1
2   8           05-10-2014  1600    SP2
3   8           15-10-2014  1300    SP1
4   8           14-12-2014  1200    SP3
------------------------------------------------------------------------------------------
创建表结构

CREATE TABLE clickpic_pricecompare.lab1 (
  ID int(11) NOT NULL AUTO_INCREMENT,
  Product_ID int(11) DEFAULT NULL,
  Price_Date date DEFAULT NULL,
  Price decimal(19, 2) DEFAULT NULL,
  Supplier varchar(255) DEFAULT NULL,
  PRIMARY KEY (ID)
)
ENGINE = MYISAM
COMMENT = 'testing-purpose';

INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(1, 8, '2014-10-26', 1300.00, 'SP1');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(2, 8, '2014-10-05', 1600.00, 'SP2');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(3, 8, '2014-10-15', 1300.00, 'SP1');
INSERT INTO  lab1(ID, Product_ID, Price_Date, Price, Supplier) VALUES
(4, 8, '2014-10-14', 1200.00, 'SP3');
我需要的结果如下所示

--------------------------------------------------------------------------------------
ID  Product_ID  Month   Price   Supplier
--------------------------------------------------------------------------------------
4   8           October 1200    SP3
-------------------------------------------------------------------------------------------

请帮助…

您可以使用带有产品id和最低价格金额的self-join来获得每个产品id的最低价格行

select l.ID,
l.Product_ID,
monthname(l.Price_Date) `Month`,
l.Price,
l.Supplier
from lab1 l
join (select Product_ID,min(Price) Price
     from lab1
     group by Product_ID) l1
using(Product_ID,Price)

我想您正在寻找:

选择l.ID、l.Product\u ID、monthname(l.Price\u Date)作为
月份
、l.Price、l.Supplier

from lab1 l join
     (select Product_ID, year(l.Price_date) as yr, month(l.Price_Date) as mon, min(Price) as Price
      from lab1
      group by Product_ID, year(l.Price_date), month(l.Price_Date)
     ) lmin
     on l.Product_id = lmin.Product_id and
        year(l.Price_Date) = lmin.yr and
        month(l.Price_Date) = lmin.mon;
如果只需要10月份的数据,请添加
where
子句:

where l.Price_Date >= '2014-10-01' and l.Price_Date < '2014-11-01'
其中l.Price_日期>='2014-10-01'和l.Price_日期<'2014-11-01'

亲爱的Khalid,这对我帮助很大。但如果我在9月份输入一些低值的数据,它只会显示该月的最低值。。。有没有办法显示每个月的最低价格?还要注意,供应商位于另一个名为supplier的表中。@riyasrawther'注意,供应商位于另一个表中'er,不,不是t@RiyasRawther你能根据需要更新你的问题吗?还有样本数据集,为了我们的方便,您将日期写回到前面了吗?我觉得那不方便!我用这个sql。。。但只显示每种产品的最低值只有一个月。。。
where l.Price_Date >= '2014-10-01' and l.Price_Date < '2014-11-01'