在SQL中,如何获取列的值在表中最低的所有行?

时间:2021-01-01 15:58:34

I am a newbie to SQL, I am using this query to look for the minimum value in the field weight of my table.

我是SQL的新手,我使用此查询来查找表的字段权重中的最小值。

SELECT product_id, 
       MIN(weight) 
  FROM table 
 WHERE 1;

It does show one field with the min value, but only one? But I have many products with the same minimum weight. Is there a way I could specify that I need to show all other products?

它确实显示一个具有最小值的字段,但只有一个?但我有很多产品具有相同的最小重量。有没有办法指明我需要展示所有其他产品?

3 个解决方案

#1


16  

select * from table where weight = (select MIN(weight) from table)

#2


3  

This may be what you're asking for:

这可能是你要求的:

SELECT product_id FROM table WHERE weight = (SELECT MIN(weight) FROM table);

As you might guess, this will select all prodict_ids where the weight is equal to the minimum weight in the table.

正如您可能猜到的,这将选择所有prodict_ids,其中权重等于表中的最小权重。

#3


1  

Not sure which one exactly you want, but either of these should do the trick:

不确定你想要哪一个,但其中任何一个应该可以解决问题:

SELECT product_id, MIN(weight) FROM table WHERE 1 GROUP BY product_id

(List all product IDs and the minimum weight per product ID)

(列出所有产品ID和每个产品ID的最小重量)

SELECT product_id, weight FROM table WHERE weight = (SELECT min(weight) FROM table)

(Find all product IDs where the weight equals the minimum weight)

(查找重量等于最小重量的所有产品ID)

SELECT min(weight) FROM table;

(Find the absolute minimum weight, and that's that)

(找到绝对最小重量,就是那个)

#1


16  

select * from table where weight = (select MIN(weight) from table)

#2


3  

This may be what you're asking for:

这可能是你要求的:

SELECT product_id FROM table WHERE weight = (SELECT MIN(weight) FROM table);

As you might guess, this will select all prodict_ids where the weight is equal to the minimum weight in the table.

正如您可能猜到的,这将选择所有prodict_ids,其中权重等于表中的最小权重。

#3


1  

Not sure which one exactly you want, but either of these should do the trick:

不确定你想要哪一个,但其中任何一个应该可以解决问题:

SELECT product_id, MIN(weight) FROM table WHERE 1 GROUP BY product_id

(List all product IDs and the minimum weight per product ID)

(列出所有产品ID和每个产品ID的最小重量)

SELECT product_id, weight FROM table WHERE weight = (SELECT min(weight) FROM table)

(Find all product IDs where the weight equals the minimum weight)

(查找重量等于最小重量的所有产品ID)

SELECT min(weight) FROM table;

(Find the absolute minimum weight, and that's that)

(找到绝对最小重量,就是那个)