我有兩個表,發票和成員,通過字段InvoiceNum通過PK/FK關係連接。我創建了下面的SQL,它工作正常,並按預期方式拖拽44條記錄。內部連接SQL語句
SELECT
INVOICES.InvoiceNum,
INVOICES.GroupNum,
INVOICES.DivisionNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
WHERE MEMBERS.MemberNum = '20032526000'
現在,我想與GroupName
和DivisionName
更換INVOICES.GroupNum
和INVOICES.DivisionNum
在上面的查詢。這些值存在於「組」和「分部」表格中,該表格也具有相應的Group_num和Division_num字段。我創建了以下sql。問題是,它現在拉528記錄,而不是44!
SELECT
INVOICES.InvoiceNum,
INVOICES.DateBillFrom,
INVOICES.DateBillTo,
DIVISIONS.DIVISION_NAME,
GROUPS.GROUP_NAME
FROM INVOICES
INNER JOIN MEMBERS ON INVOICES.InvoiceNum = MEMBERS.InvoiceNum
INNER JOIN GROUPS ON INVOICES.GroupNum = GROUPS.Group_Num
INNER JOIN DIVISIONS ON INVOICES.DivisionNum = DIVISIONS.Division_Num
WHERE MEMBERS.MemberNum = '20032526000'
任何幫助,非常感謝。
按組數有多個組的名稱?部門一樣嗎? – Taryn
看起來你在成員/發票和團隊/部門之間有多對一的關係。您需要查看並決定是否要退回發票或組或分部 –
我會在提問時將您的代碼放入SQL Fiddle,http://www.sqlfiddle.com/中,並共享該鏈接。這會讓人們更容易看到潛在的關係。 –