1

我有 2 张桌子:

一张桌子包含不同的笔记本电脑规格

laptop:
|partNumber|picture|laptopName|laptopProcessor|visibility|
..........................................................
|AB00001   |pic1   |lenovo..  |intel core I5  |1         |
|AB00002   |pic2   |samsung   |AMD semprom 3.8|1         |

另一张表是库存表,我有多家商店,每家商店针对特定笔记本电脑的价格不同

inventory:
|partNumber|shopName|price|qty|
...............................
|AB00001   |shop1   |554  |2  |
|AB00002   |shop1   |356  |4  |
|AB00002   |shop3   |400  |1  |
|AB00001   |shop4   |625  |5  |
|AB00002   |shop4   |345  |3  |

我想写一个查询,列出价格最优惠的笔记本电脑,所以我写了这个:

SELECT laptop.partNumber, laptop.picture, laptop.laptopName, laptop.laptopProcessor,
       inventory.MIN(price)
FROM laptop INNER JOIN inventory ON inventory.partNumber=laptop.partNumber
WHERE visibility="1"

所以基本上我想以最优惠的价格显示所有笔记本电脑,它在没有价格查询的情况下工作,但我无法将此 MIN() 函数添加到查询中,我想我错过了语法,但我不知道应该如何工作。请帮我。

4

3 回答 3

1

您缺少 Group BY

SELECT laptop.partNumber, laptop.picture, laptop.laptopName, laptop.laptopProcessor,
       inventory.MIN(inventory.price)
FROM laptop INNER JOIN inventory ON inventory.partNumber=laptop.partNumber
WHERE visibility="1"
GROUP BY laptop.partNumber, laptop.picture, laptop.laptopName, laptop.laptopProcessor
于 2013-10-24T17:07:27.377 回答
1

尝试这个:

SELECT laptop.partNumber,
  laptop.picture,
  laptop.laptopName,
  laptop.laptopProcessor,
  MIN(inventory.price)
FROM laptop
INNER JOIN inventory ON inventory.partNumber = laptop.partNumber
WHERE visibility = "1"
GROUP BY laptop.partNumber,
  laptop.picture,
  laptop.laptopName,
  laptop.laptopProcessor
于 2013-10-24T17:06:11.850 回答
1

语法是MIN(column name),所以应该是MIN(inventory.price)。但是你还需要添加一个GROUP BY子句,否则整个表只有一个结果。

然后,由于您想按笔记本电脑分组,请添加GROUP BY inventory.partNumber. 如果partNumber不是表的主键laptop还应该添加其他列。

于 2013-10-24T17:06:15.373 回答