简体   繁体   English

MySQL:获取具有最小值的完整行

[英]MySQL: Get the full row with min value

I am trying to get the complete row with the lowest price, not just the field with the lowest price. 我正在尝试以最低的价格获得完整的行,而不仅仅是价格最低的字段。

Create table: 创建表:

CREATE TABLE `Products` (
  `SubProduct` varchar(100),
  `Product` varchar(100),
  `Feature1` varchar(100),
  `Feature2` varchar(100),
  `Feature3` varchar(100),
  `Price1` float,
  `Price2` float,
  `Price3` float,
  `Supplier` varchar(100)
);

Insert: 插入:

INSERT INTO
  `Products` (`SubProduct`, `Product`, `Feature1`, `Feature2`, `Feature3`, `Price1`, `Price2`, `Price3`, `Supplier`)
VALUES
  ('Awesome', 'Product', 'foo', 'foo', 'foor', '1.50', '1.50', '0', 'supplier1'),
  ('Awesome', 'Product', 'bar', 'foo', 'bar', '1.25', '1.75', '0', 'supplier2');

Select: 选择:

SELECT
  `SubProduct`,
  `Product`,
  `Feature1`,
  `Feature2`,
  `Feature3`,
  MIN(`Price1`),
  `Price2`,
  `Price3`,
  `Supplier`
FROM `Products`
  GROUP BY `SubProduct`, `Product`
  ORDER BY `SubProduct`, `Product`;

You can see that at http://sqlfiddle.com/#!2/c0543/1/0 您可以在http://sqlfiddle.com/#!2/c0543/1/0上看到

I get the frist inserted row with the content of the column price1 from the second inserted row. 我从插入的第二行中获得第一行插入的行,其中包含price1列的内容。

I expect to get the complete row with the right features, supplier and other columns. 我希望获得具有正确功能,供应商和其他列的完整行。 In this example it should be the complete second inserted row, because it has the lowest price in column price1. 在此示例中,它应该是完整插入的第二行,因为它的价格在列price1中是最低的。

You need to get the MIN price rows and then JOIN those rows with the main table, like this: 您需要获取MIN价格行,然后将这些行与主表联接,如下所示:

SELECT
  P.`SubProduct`,
  P.`Product`,
  P.`Feature1`,
  P.`Feature2`,
  P.`Feature3`,
  `Price` AS Price1,
  P.`Price2`,
  P.`Price3`,
  P.`Supplier`
FROM `Products` AS P JOIN (
    SELECT `SubProduct`, `Product`, MIN(`Price1`) AS Price
    FROM `Products`
    GROUP BY `SubProduct`, `Product`
  ) AS `MinPriceRows`
ON P.`SubProduct` = MinPriceRows.`SubProduct`
AND P.`Product` = MinPriceRows.`Product`
AND P.Price1 = MinPriceRows.Price
ORDER BY P.`SubProduct`, P.`Product`;

Working Demo: http://sqlfiddle.com/#!2/c0543/20 工作演示: http : //sqlfiddle.com/#!2/c0543/20

Here what I have done is to get a temporary recordset as MinPriceRows table which will give you MIN price per SubProduct and Product. 在这里,我要做的是获得一个临时记录集作为MinPriceRows表,该表将为您提供每个子产品和产品的MIN价格。 Then I am joining these rows with the main table so that main table rows can be reduced to only those rows which contain MIN price per SubProduct and Product. 然后,我将这些行与主表连接起来,以便可以将主表行减少为仅包含每个SubProduct和Product MIN价格的行。

Try with this: 试试这个:

SELECT
  `p`.`SubProduct`,
  `p`.`Product`,
  `p`.`Feature1`,
  `p`.`Feature2`,
  `p`.`Feature3`,
 `p`.`Price1`,
  `p`.`Price2`,
  `p`.`Price3`,
  `p`.`Supplier`
FROM `Products` `p`
inner join (select MIN(`Price1`)as `Price1`
            From `Products`
           ) `a` on `a`.`Price1` = `p`.`Price1`

ORDER BY `p`.`SubProduct`, `p`.`Product`;

demo: http://sqlfiddle.com/#!2/c0543/24 演示: http : //sqlfiddle.com/#!2/c0543/24

This works... 这有效...

SELECT
P1.`SubProduct`,
P1.`Product`,
P1.`Feature1`,
P1.`Feature2`,
P1.`Feature3`,
P1.`Price1`,
P1.`Price2`,
P1.`Price3`,
P1.`Supplier`
FROM `Products` P1
INNER JOIN `Products` P2 ON  P1.SubProduct = P2.SubProduct AND P1.Product = P2.Product
WHERE P1.Price1 < P2.Price1

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

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