主/外键;使用子查询连接多个表

时间:2019-07-16 03:53:58

标签: mysql join subquery

我有一个分配5张桌子的问题,如下所示。我需要用每种运动的最低费用写一个查询:

enter image description here

enter image description here

第二列是equipment_name

enter image description here

enter image description here

enter image description here

我认为我需要在子查询中进行一堆联接,主键为id列,外键为name_id列。这是正确的方法吗?

2 个答案:

答案 0 :(得分:2)

您不需要一堆联接;至少可以通过在store_equipment_price和sports_equipment表之间进行一次连接来解决此问题-如果将这两个设备ID连接在一起,则您将有效地获得行数,该行数可以为每个商店的每种运动提供启动成本。您需要按运动ID和商店ID进行分组;不要忘记通过从商店A获得所有装备来开始踢足球可能会更便宜,但是通过撕掉B来开始打高尔夫球可能会更便宜-我来读这个问题。但是,如果您准备从商店A拿起手套,从商店B等拿起蝙蝠,那么在求和时我们甚至没有按商店分组,而是我们算出哪个商店对每个组件最便宜,而不是哪个商店最便宜每项运动的整体。

如果您要在结果行上生成命名的商店/体育,那么您将需要更多的联接,但是请尝试以最少的联接数开始以正确的结果开始

通过使用解析/窗口函数,这两个查询最终都将变得更加容易,但是它们依赖于数据库;永远不要在没有说明数据库供应商的情况下发布sql问题,因为很少有问题是纯ISO SQL

答案 1 :(得分:1)

您的问题尚不完全清楚,我认为您需要找到可以从商店购买所有运动用品的商店,以产生最低费用。以下查询将实现这一目标

select s.sports, e.equipment_name, min(sep.price),
(select store_name from stores st where st.id = sep.store_id) store_name
from sports s
join sports_equipment se on s.id = se.sport_id
join equipment e on e.id = se.equipment_id 
join sports_equipment_prices sep on sep.equipment_id = se.equipment_id
group by s.sports, e.equipment_name
order by s.sports, e.equipment_name
;

以下“创建表” “插入数据” 脚本基于您的屏幕图像

create table sports (
    id     INTEGER PRIMARY KEY AUTOINCREMENT,
    sports varchar(50)
);

insert into sports(sports) values('golf');    
insert into sports(sports) values('baseball');
insert into sports(sports) values('soccer');

create table stores (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    store_name     varchar(50)
);

insert into stores(store_name) values('A');
insert into stores(store_name) values('B');
insert into stores(store_name) values('C');

create table equipment (
    id                 INTEGER PRIMARY KEY AUTOINCREMENT,
    equipment_name     varchar(50)
);

insert into equipment(equipment_name) values('shoes');
insert into equipment(equipment_name) values('ball');
insert into equipment(equipment_name) values('clubs');
insert into equipment(equipment_name) values('glove');
insert into equipment(equipment_name) values('bat');

create table sports_equipment (
    sport_id         INTEGER not null,
    equipment_id     INTEGER not null,
    FOREIGN KEY(sport_id) REFERENCES sports(id),
    FOREIGN KEY(equipment_id) REFERENCES equipment(id)
);

insert into sports_equipment values(1, 1);
insert into sports_equipment values(1, 2);
insert into sports_equipment values(1, 3);
insert into sports_equipment values(2, 2);
insert into sports_equipment values(2, 4);
insert into sports_equipment values(2, 5);
insert into sports_equipment values(3, 1);
insert into sports_equipment values(3, 2);

create table sports_equipment_prices (
    id                 INTEGER PRIMARY KEY AUTOINCREMENT,
    store_id           INTEGER not null,
    equipment_id       INTEGER not null,
    price              INTEGER not null,  
    FOREIGN KEY(store_id) REFERENCES stores(id),
    FOREIGN KEY(equipment_id) REFERENCES equipment(id)
);