如何从不同的表中获取值,我的唯一引用是外键? [英] How do I grab values out of different tables with my only reference being the foreign key?
问题描述
嘿!
我现在想说抱歉因为这个问题可能会变得有点混乱,英语不是我的母语而且我'我不习惯像这样制定更长的文字。
好吧,假设我有5张桌子
表1
Hey!
I want to say sorry right now because this question might become a little bit of a mess, english isn't my native language and I'm not used to formulating longer text's like this.
Alright, let's say I have 5 tables
Table 1
PKTable1 | FKTable3 | Quantity | Position
表2
Table 2
PKTable2 | Date | Status | FKTable4
表3
Table 3
PKTable3 | ArticleName | Price
表4
Table 4
PKTable4| Name | FirstName
表5
Table 5
PKTable5 | FKTable1 | FKTable2
我能以某种方式从表4获取Table4中的Name和FirstName吗?仅使用Table5?
我正在使用C#,LinQ和EntityFramework,如果这有帮助
我尝试过:
这更像是一个这是可能的问题,而不是一个与代码直接相关的问题,所以我没有什么可以在这里遗憾地显示出来。
Can I somehow fetch for example Name and FirstName from Table4 only using Table5?
I'm using C#, LinQ and EntityFramework if that helps
What I have tried:
This is more of a 'is this possible' question, rather than one directly related to code so I've nothing to show here sadly.
推荐答案
是:
Yes:
SELECT t4.Name, t4.FirstName FROM Table5 t5
JOIN Table2 t2 ON t5.FKTable2 = t2.PKTable2
JOIN Table4 t4 ON t4.PKTable4 = t2.FKTable4
在SSMS中尝试它,你会看到它的作用。
看一下JOIN的解释: SQL连接 [ ^ ]
Try it in SSMS and you'll see what it does.
Have a look at JOIN for an explanation: SQL Joins[^]
这篇关于如何从不同的表中获取值,我的唯一引用是外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!