我有三张桌子:
Shop_Table
shop_id
shop_name
Sells_Table
shop_id
item_id
price
Item_Table
item_id
item_name
通过Sells_Table
FK 将项目和商店表链接到它们的 ID。我正在尝试从每家商店获取最昂贵的商品,即表单的输出:
(shop_name, item_name, price)
(shop_name, item_name, price)
(shop_name, item_name, price)
(shop_name, item_name, price)
...
其中价格是每个商店的最高价格项目。我似乎可以实现 (shop_name, max(price)) 但是当我尝试包含 item_name 时,我得到了 shop_name 的多个条目。我目前的方法是
create view shop_sells_item as
select s.shop_name as shop, i.item_name as item, price
from Shop_Table s
join Sells_Table on (s.shop_id = Sells_Table.shop_id)
join Item_Table i on (i.item_id = Sells_Table.item_id);
select shop, item, max(price)
from shop_sells_item
group by shop;
但是,我收到一条错误消息item must appear in the GROUP BY clause or be used in an aggregate function
,但如果我包含它,那么我不会获得每家商店的最高价格,而是获得每家商店的最高价格,这是没有用的物品对。
另外,使用视图是最好的方法吗?可以通过单个查询完成吗?