当我加入两个表时,我对如何构建SQL查询感到困惑,以便我以后可以将它们绑定到gridview。
基本上我有两张桌子,
tablename: family
__________________________
|Status |lastName|firstName|
|-------|--------|---------|
|1 |11 |111 |
|2 |22 |222 |
|3 |33 |333 |
''''''''''''''''''''''''''''
+
tablename: familyStatus
_____________________
|id | Notes |
|----------|----------|
|1 | 1111 |
|2 | 2222 |
|3 | 3333 |
'''''''''''''''''''''''
=
Joined-tables: family+familyStatus [this is what i want]
____________________________
|firstName | lastName |Notes |
|----------|----------|------|
|111 | 11 |1111 |
|222 | 22 |2222 |
|333 | 33 |3333 |
''''''''''''''''''''''''''''''
您在此处看到的是 familyStatus 表中系列的状态和 id 是同一个键,status和id将隐藏在gridview上。
Lets just assume for now that this is how i get the family table.
SELECT Status, firstName, lastName
FROM family
WHERE firstName= @fN
AND lastName = @lN
我需要在此添加什么才能使来自族表的Notes也加入?
答案 0 :(得分:6)
加入:
SELECT F.firstName, F.lastName, FS.Notes
FROM family F
INNER JOIN familyStatus FS
ON F.[Status] = FS.id
WHERE F.firstName= @fN
AND F.lastName = @lN
答案 1 :(得分:1)
将Status外键添加到familyStatus表并尝试以下操作:
SELECT firstName, lastName, Notes
FROM family JOIN familyStatus
ON family.Status = familyStatus.Status
WHERE firstName= @fN
AND lastName = @lN