Select 关系数据库的最低价格
Select min price from relational database
我正在尝试 select 列出每个产品的价格可能最低的产品列表。为了说明上下文,我有这些 tables:
**tb_product**
id | product | active | image | description
1 | shoes A | 1 | ... | ...
2 | shirt C | 1 | ... | ...
4 | pants E | 1 | ... | ...
---
**tb_store**
id | name | phone
11 | Store X | ...
22 | Store F | ...
33 | Store K | ...
---
**tb_product_store**
id_product | id_store | price
1 | 11 | 9.90
1 | 22 | 12.90
2 | 11 | 15.90
2 | 33 | 12.90
4 | 33 | 22.90
---
例如,我想 select table tb_product_store
上列出的所有产品的价格最低,所以结果将是这样的:
id_product | product | price
1 | shoes A | 9.90
2 | shirt C | 12.90
4 | pants E | 22.90
我有这个查询:
SELECT
a.id, a.product,
MIN(b.price) as 'price'
FROM tb_product a
JOIN tb_product_store b ON b.id_product = a.id
WHERE a.active = 1
ORDER BY a.product
但是这个查询 returns 只有一个产品的最低价格,而不是包含所有产品和最低价格的列表。
怎样才能得到预期的结果?
SELECT a.id, a.product,
b.price as 'price'
FROM tb_product a
join (select id_product, min(price)
from tb_product_store group by id_product) b
on b.id_product = a.id
whera a.active = 1
order by a.product
我正在尝试 select 列出每个产品的价格可能最低的产品列表。为了说明上下文,我有这些 tables:
**tb_product**
id | product | active | image | description
1 | shoes A | 1 | ... | ...
2 | shirt C | 1 | ... | ...
4 | pants E | 1 | ... | ...
---
**tb_store**
id | name | phone
11 | Store X | ...
22 | Store F | ...
33 | Store K | ...
---
**tb_product_store**
id_product | id_store | price
1 | 11 | 9.90
1 | 22 | 12.90
2 | 11 | 15.90
2 | 33 | 12.90
4 | 33 | 22.90
---
例如,我想 select table tb_product_store
上列出的所有产品的价格最低,所以结果将是这样的:
id_product | product | price
1 | shoes A | 9.90
2 | shirt C | 12.90
4 | pants E | 22.90
我有这个查询:
SELECT
a.id, a.product,
MIN(b.price) as 'price'
FROM tb_product a
JOIN tb_product_store b ON b.id_product = a.id
WHERE a.active = 1
ORDER BY a.product
但是这个查询 returns 只有一个产品的最低价格,而不是包含所有产品和最低价格的列表。
怎样才能得到预期的结果?
SELECT a.id, a.product,
b.price as 'price'
FROM tb_product a
join (select id_product, min(price)
from tb_product_store group by id_product) b
on b.id_product = a.id
whera a.active = 1
order by a.product