查询 tables 中 FK 在 junction table 中引用的其他字段
Query other fields in tables referenced by FK in junction table
我有一个 "junction" table (recipe_ingredients
),我用它来处理数据库中的多对多关系。 table 中的每个字段都是对另一个 table 的 PK 的引用:recipe_id
、ingredient_id
、quantity_id
和 measure_id
。这些是 metadata
、ingredients
、quantities
和 measurements
table 的 PK。除了 metadata
table,所有这些 table 都只有两个字段:PK,以及与该 PK 关联的名称或号码。
我正在尝试查询 recipe_ingredients
具有给定 recipe_id
的所有行...但我不想在每一行中显示具有 recipe_id
的主键在 table.
中显示与该 PK 关联的 name/number
所以不是查询 returns 这个:
recipe_id | ingredient_id | quantity_id | measure_id
----------+---------------+-------------+-----------
1 | 10 | 2 | 3
----------+---------------+-------------+-----------
1 | 12 | 2 | 3
----------+---------------+-------------+-----------
1 | 13 | 5 | 6
----------+---------------+-------------+-----------
...
我正在寻找 returns 类似这样的查询:
recipe_id | ingredient_name | quantity_num | measure_num
----------+-----------------+--------------+------------
1 | Ground cayenne | 1/2 | tsp
----------+-----------------+--------------+------------
1 | Ground paprika | 1/2 | tsp
----------+-----------------+--------------+------------
1 | Olive oil | 1 | tbsp
----------+-----------------+--------------+------------
...
我刚开始学习SQL,所以我只知道如何对单个table进行简单的查询。我什至不知道从哪里开始这个查询,只是我可能需要一种 join
语句。我可以写什么查询来实现这个?
我在 Debian 上使用 sqlite3
。
recipe_ingredients"junction"table:
CREATE TABLE recipe_ingredients (
recipe_id int,
ingredient_id int,
quantity_id int,
measure_id int,
primary key (recipe_id, ingredient_id),
foreign key (recipe_id)
references metadata (recipe_id)
on update restrict
on delete restrict,
foreign key (ingredient_id)
references ingredients (ingredient_id)
on update restrict
on delete restrict,
foreign key (quantity_id)
references quantities (quantity_id)
on update restrict
on delete restrict,
foreign key (measure_id)
references measurements (measure_id)
on update restrict
on delete restrict
);
这些是结点 table:
引用的外部 table
create table if not exists ingredients (
ingredient_id integer primary key,
ingredient_name text not null
);
create table if not exists quantities (
quantity_id integer primary key,
quantity_num int not null
);
create table if not exists measurements (
measure_id integer primary key,
measure_name text not null
);
create table if not exists metadata (
recipe_id integer primary key,
recipe_name text not null,
course_id int,
cuisine_id int,
servings int,
prep_time int,
cook_time int,
total_time int,
foreign key (course_id)
references courses (course_id)
on update restrict
on delete restrict,
foreign key (cuisine_id)
references cuisine (cuisine_id)
on update cascade
on delete cascade
);
您可以 join
连接点 table 与每个相关的 table 像这样:
select
ri.recipe_id,
i.ingredient_name,
q.quantity_num,
m.measure_num
from recipe_ingredients ri
inner join ingredients i on i.ingredient_id = ri.ingredient_id
inner join quantities q on q.quantity_id = ri.quantity_id
inner noin measurements m on m.measure_id = ri.measure_id
where ri.recipe_id = ?
问号(?
)代表您要查找的recipe_id
。
我有一个 "junction" table (recipe_ingredients
),我用它来处理数据库中的多对多关系。 table 中的每个字段都是对另一个 table 的 PK 的引用:recipe_id
、ingredient_id
、quantity_id
和 measure_id
。这些是 metadata
、ingredients
、quantities
和 measurements
table 的 PK。除了 metadata
table,所有这些 table 都只有两个字段:PK,以及与该 PK 关联的名称或号码。
我正在尝试查询 recipe_ingredients
具有给定 recipe_id
的所有行...但我不想在每一行中显示具有 recipe_id
的主键在 table.
所以不是查询 returns 这个:
recipe_id | ingredient_id | quantity_id | measure_id
----------+---------------+-------------+-----------
1 | 10 | 2 | 3
----------+---------------+-------------+-----------
1 | 12 | 2 | 3
----------+---------------+-------------+-----------
1 | 13 | 5 | 6
----------+---------------+-------------+-----------
...
我正在寻找 returns 类似这样的查询:
recipe_id | ingredient_name | quantity_num | measure_num
----------+-----------------+--------------+------------
1 | Ground cayenne | 1/2 | tsp
----------+-----------------+--------------+------------
1 | Ground paprika | 1/2 | tsp
----------+-----------------+--------------+------------
1 | Olive oil | 1 | tbsp
----------+-----------------+--------------+------------
...
我刚开始学习SQL,所以我只知道如何对单个table进行简单的查询。我什至不知道从哪里开始这个查询,只是我可能需要一种 join
语句。我可以写什么查询来实现这个?
我在 Debian 上使用 sqlite3
。
recipe_ingredients"junction"table:
CREATE TABLE recipe_ingredients (
recipe_id int,
ingredient_id int,
quantity_id int,
measure_id int,
primary key (recipe_id, ingredient_id),
foreign key (recipe_id)
references metadata (recipe_id)
on update restrict
on delete restrict,
foreign key (ingredient_id)
references ingredients (ingredient_id)
on update restrict
on delete restrict,
foreign key (quantity_id)
references quantities (quantity_id)
on update restrict
on delete restrict,
foreign key (measure_id)
references measurements (measure_id)
on update restrict
on delete restrict
);
这些是结点 table:
引用的外部 tablecreate table if not exists ingredients (
ingredient_id integer primary key,
ingredient_name text not null
);
create table if not exists quantities (
quantity_id integer primary key,
quantity_num int not null
);
create table if not exists measurements (
measure_id integer primary key,
measure_name text not null
);
create table if not exists metadata (
recipe_id integer primary key,
recipe_name text not null,
course_id int,
cuisine_id int,
servings int,
prep_time int,
cook_time int,
total_time int,
foreign key (course_id)
references courses (course_id)
on update restrict
on delete restrict,
foreign key (cuisine_id)
references cuisine (cuisine_id)
on update cascade
on delete cascade
);
您可以 join
连接点 table 与每个相关的 table 像这样:
select
ri.recipe_id,
i.ingredient_name,
q.quantity_num,
m.measure_num
from recipe_ingredients ri
inner join ingredients i on i.ingredient_id = ri.ingredient_id
inner join quantities q on q.quantity_id = ri.quantity_id
inner noin measurements m on m.measure_id = ri.measure_id
where ri.recipe_id = ?
问号(?
)代表您要查找的recipe_id
。