我有一个看起来微不足道的问题,但我无法让它发挥作用。它在 Oracle SQL 中。这是脚本的示例:
create table product (
product_id number primary key,
name varchar(255)
);
create table producer (
producer_id number primary key,
name varchar(255)
);
create table catalog (
pp_product_id number,
pp_producer_id number,
price number
);
alter table catalog add constraint pp_product_id1 foreign key (pp_product_id) references product (product_id);
alter table catalog add constraint pp_product_id2 foreign key (pp_producer_id) references producer (producer_id);
insert into product (product_id, name) values (1, 'HDD 250 gb');
insert into product (product_id, name) values (2, 'HDD 500 gb');
insert into product (product_id, name) values (3, 'HDD 750 gb');
insert into producer (producer_id, name) values (1, 'Hitachi');
insert into producer (producer_id, name) values (2, 'Corsair');
insert into producer (producer_id, name) values (3, 'Western Digital');
insert into catalog (pp_product_id, pp_producer_id, price) values (1,1, 80);
insert into catalog (pp_product_id, pp_producer_id, price) values (1,3, 60);
insert into catalog (pp_product_id, pp_producer_id, price) values (2,1, 75);
insert into catalog (pp_product_id, pp_producer_id, price) values (2,2, 40);
insert into catalog (pp_product_id, pp_producer_id, price) values (3,2, 63);
insert into catalog (pp_product_id, pp_producer_id, price) values (3,3, 100);
因此,总共有 6 个产品,3 个生产商,3 个产品类别(HD 类型)。我需要的是显示最便宜的产品,每种类型的产品都带有与该产品相关的生产商名称。就像是:
西部数据 250 GB 60
海盗船 500 GB 40
海盗船 750 GB 63
这将在他们的组(类型)中选择最便宜的高清
select p.name, min (c.price)
from product p, catalog c, producer prc
where c.pp_product_id = p.product_id and prc.producer_id = c.pp_producer_id
group by p.name;
...但我不能添加生产者姓名?我尝试了几种解决方案,但都没有奏效。