如何 select 基于多个外键的数据库条目?
How to select a database entry based on multiple foreign keys?
我对关系数据库还很陌生。我有一个 postgresql table,其价格基于多个外键:
price age_id type_id code_id
8.9 5 3 8
... ... ... ...
所以age_id、type_id和code_id是外键并指向条目的值。它们存储在单独的 tables:
age_id age
... ...
5 49
... ...
和
type_id type
... ...
3 FAM
... ...
和
code_id code
... ...
8 769894
... ...
如何根据年龄、类型和代码的值获取价格条目(例如 8.9)?我必须获得每个 table 的 ID,然后 select 来自主 table 的条目,对吧?
我不知道如何有效地解决这个问题。提前谢谢你。
使用 3 个内部联接:
SELECT price
FROM price_table
INNER JOIN age_table
ON age_table.id = price.age_id
INNER JOIN type_table
ON type_table.id = price.type_id
INNER JOIN code_table
ON code_table.id = price.code_id
WHERE age = 49
AND type = 'FAM'
AND code = 769894
我建议检查以下内容:
https://www.w3schools.com/sql/sql_join.asp
我对关系数据库还很陌生。我有一个 postgresql table,其价格基于多个外键:
price age_id type_id code_id
8.9 5 3 8
... ... ... ...
所以age_id、type_id和code_id是外键并指向条目的值。它们存储在单独的 tables:
age_id age
... ...
5 49
... ...
和
type_id type
... ...
3 FAM
... ...
和
code_id code
... ...
8 769894
... ...
如何根据年龄、类型和代码的值获取价格条目(例如 8.9)?我必须获得每个 table 的 ID,然后 select 来自主 table 的条目,对吧? 我不知道如何有效地解决这个问题。提前谢谢你。
使用 3 个内部联接:
SELECT price
FROM price_table
INNER JOIN age_table
ON age_table.id = price.age_id
INNER JOIN type_table
ON type_table.id = price.type_id
INNER JOIN code_table
ON code_table.id = price.code_id
WHERE age = 49
AND type = 'FAM'
AND code = 769894
我建议检查以下内容: https://www.w3schools.com/sql/sql_join.asp