Mysql 每种产品的最低/最高价格(查询) 目标
选择数据库中每个产品的最低/最高价格 问题 我只能获得具有指定标识符的产品 我所拥有的 我正在使用MySQL,我有以下查询:Mysql 每种产品的最低/最高价格(查询) 目标,mysql,sql,Mysql,Sql,选择数据库中每个产品的最低/最高价格 问题 我只能获得具有指定标识符的产品 我所拥有的 我正在使用MySQL,我有以下查询: SELECT (MIN(`map`.`Product_Price`)) as `minProductPrice`, (MAX(`map`.`Product_Price`)) as `maxProductPrice`, `pr`.`Product_Name` as `productName` FROM `bm_market_products` as `map
SELECT (MIN(`map`.`Product_Price`)) as `minProductPrice`,
(MAX(`map`.`Product_Price`)) as `maxProductPrice`,
`pr`.`Product_Name` as `productName`
FROM `bm_market_products` as `map`
JOIN `bm_products` as `pr`
JOIN `bm_markets` as `ma`
WHERE `map`.`Product_Id` = 1
AND `map`.`Product_Id` = `pr`.`Product_Id`
我返回的是minProductPrice
、maxProductPrice
和productName
解决方案
谢谢你的帮助。上面的两个答案都是正确的,但我选择了@GordonLinoff答案作为被接受的答案,因为我认为它会更有用,更受初学者的喜爱,但真的要感谢你们两位最终查询:
SELECT MIN(`map`.`Product_Price`) as `minProductPrice`,
MAX(`map`.`Product_Price`) as `maxProductPrice`,
`pr`.`Product_Name` as `productName`
FROM `bm_market_products` `map` join
`bm_products` as `pr`
on map`.`Product_Id` = `pr`.`Product_Id`
group by `map`.`Product_Id`
干杯 首先,当您使用
join
时,应该始终有一个on
子句,即使MySQL不需要它。如果您想要一个交叉联接
,那么请明确说明
SELECT (MIN(`map`.`Product_Price`)) as `minProductPrice`,
(MAX(`map`.`Product_Price`)) as `maxProductPrice`,
`pr`.`Product_Name` as `productName`,
`map`.`Product_Id`
FROM `bm_market_products` as `map`
JOIN `bm_products` as `pr`
JOIN `bm_markets` as `ma`
WHERE `map`.`Product_Id` = `pr`.`Product_Id`
GROUP BY `map`.`Product_Id`
其次,在查询中根本不使用tm_markets
表。它不是必需的,所以请将其删除
生成的查询应该可以工作:
SELECT MIN(`map`.`Product_Price`) as `minProductPrice`,
MAX(`map`.`Product_Price`) as `maxProductPrice`,
`pr`.`Product_Name` as `productName`
FROM `bm_market_products` `map` join
`bm_products` as `pr`
on map`.`Product_Id` = `pr`.`Product_Id`
WHERE `map`.`Product_Id` = 1
由于您只选择一种产品,因此可能不需要使用分组方式。然而,你可能会考虑这个问题:
SELECT MIN(`map`.`Product_Price`) as `minProductPrice`,
MAX(`map`.`Product_Price`) as `maxProductPrice`,
`pr`.`Product_Name` as `productName`
FROM `bm_market_products` `map` join
`bm_products` as `pr`
on map`.`Product_Id` = `pr`.`Product_Id`
group by `map`.`Product_Id`
这将返回所有产品的信息。您是否只想获得最低和最高产品价格?如果是这样,您可以通过删除WHERE map.Product\u Id=1
来实现。或者,您是否也在尝试获取产品信息?例如,对于价格最高的产品,product\u Id
和/或ProductName
,对于价格最低的产品,product\u Id
和/或ProductName
?您好,@EdGibbs。事实上,我正在尝试获取产品的最低和最高价格以及数据库中所有商品的相关信息。谢谢@chiefGui。也很抱歉-我错过了戈登林诺夫在下面指出的交叉连接。这两个答案中的任何一个都能满足您的需要。@EdGibbs抱歉?为什么?呵呵。。。你是想帮我。谢谢你的合作。干杯嘿,乔!你的答案对我来说很有效,我给了你+1,但我接受了@GordonLinoff的答案,因为它对初学者来说更完整。无论如何,非常感谢你+1:哇!谢谢你的回答,亲爱的朋友-对我很有用。谢谢!+1/接受!