Join
左連接不起作用 - 不是在談論我的骨頭
我有兩張桌子
dbo. 字母:
- 字母ID
- 字母標題
- 字母正文
- 電子表格
- eletter_teacherID
- 電子簽名
- eletter_parentID
dbo.狀態:
- status_id
- status_letrid
- status_parentid
- status_status
以及以下查詢:
SELECT eletter_id, eletter_form, status_letterid, status_parentid FROM dbo.eletter T1 LEFT OUTER JOIN dbo.status T2 ON T2.status_letterid = T1.eletter_id
以上給了我
|------------+--------------+-----------------+-----------------| | eletter_id | eletter_form | status_letterid | status_parentid | |------------+_-------------+-----------------+-----------------| | 1 | Year 5 | 1 | 3 | |------------+-+------------+-----------------+-----------------| | 2 | Year 7 | | | |------------+-+------------+-----------------+-----------------| | 3 | Year 8 | | | |------------+-+------------+-----------------+-----------------| | 4 | Year 6 | | | |------------+-+------------+-----------------+-----------------| | 5 | Year 5 | 5 | 1 | |------------+-+------------+-----------------+-----------------| | 1 | Year 5 | 6 | 3 | |------------+-+------------+-----------------+-----------------|
我想要的輸出是第 5 年的 parentid 3 的所有字母
|------------+--------------+-----------------+-----------------| | eletter_id | eletter_form | status_letterid | status_parentid | |------------+_-------------+-----------------+-----------------| | 1 | Year 5 | 1 | 3 | |------------+-+------------+-----------------+-----------------| | 2 | Year 7 | | | |------------+-+------------+-----------------+-----------------| | 3 | Year 8 | | | |------------+-+------------+-----------------+-----------------| | 4 | Year 6 | | | |------------+-+------------+-----------------+-----------------| | 5 | Year 5 | 5 | 1 | |------------+-+------------+-----------------+-----------------| | 1 | Year 5 | 6 | 3 | |------------+-+------------+-----------------+-----------------|
總共應該是3條記錄
感謝您的幫助
您的問題不太清楚 - 有 2 行,而不是 3 行,其 parentid = 3 和 year = ‘Year 5’。
INNER JOIN
因此,如果您只想要在 中包含條目的記錄,則可能需要使用T2
:SELECT eletter_id, eletter_form, status_letterid, status_parentid FROM dbo.eletter T1 INNER JOIN dbo.status T2 ON T2.status_letterid = T1.eletter_id;
或者您的意思是查詢應該為您提供特定的 parentid 和年份值,在這種情況下,您可能希望
WHERE
在查詢的子句中指定這些值:SELECT eletter_id, eletter_form, status_letterid, status_parentid FROM dbo.eletter T1 LEFT OUTER JOIN dbo.status T2 ON T2.status_letterid = T1.eletter_id WHERE T1.eletter_form = 'Year 5' AND T2.status_parentid = 3;