2

在连接两个表时,我对如何构建 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是相同的键,状态和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

我需要在此处添加什么以便家庭表中的注释也被加入?

4

2 回答 2

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
于 2012-11-14T14:00:46.483 回答
1

将 Status 外键添加到 familyStatus 表并尝试以下操作:

SELECT firstName, lastName, Notes
FROM family JOIN familyStatus
ON family.Status =  familyStatus.Status
WHERE firstName= @fN
AND lastName = @lN
于 2012-11-14T14:00:29.847 回答