找到你要的答案

Q:Find lowest value from the rows which contains similar foreign key value

Q:从包含类似外键值的行中查找最低值

Table:pk   fk   price 1      1      23 2      1      12 3      1      3 4      2      53 5      2      75 6      3      95 7      3      113 8      3      63 9      3      73 10     3      93 11     4      113 11     4      150 11     4      105

In the above table: How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.

I want a single SQL statement which could find lowest price for each common fk value.

Table:pk   fk   price 1      1      23 2      1      12 3      1      3 4      2      53 5      2      75 6      3      95 7      3      113 8      3      63 9      3      73 10     3      93 11     4      113 11     4      150 11     4      105

In the above table: How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.

我想要一个单独的SQL语句,可以找到最低的价格为每一个普通的FK值。

answer1: 回答1:

You just want a basic aggregate per group.

select fk, min(price)
  from your_table
  group by fk;

你只需要一个基本的集合。

select fk, min(price)
  from your_table
  group by fk;
mysql  optimization