我对关系数据库很陌生。我有一个 postgresql 表,其价格基于多个外键:
price age_id type_id code_id
8.9 5 3 8
... ... ... ...
因此 age_id、type_id 和 code_id 是外键并指向条目的值。它们存储在单独的表中:
age_id age
... ...
5 49
... ...
和
type_id type
... ...
3 FAM
... ...
和
code_id code
... ...
8 769894
... ...
如何根据年龄、类型和代码的值获取价格条目(例如 8.9)?我必须获取每个表的 ID,然后从主表中选择条目,对吗? 我不知道如何有效地解决这个问题。提前谢谢你。
最佳答案
使用 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
关于sql - 如何根据多个外键选择数据库条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54310830/