我試圖顯示已交付給成員的已購書籍的列表。該清單應顯示會員識別號碼,名字和姓氏,地址,聯繫電話,書籍序列號,書名,數量和交付日期。SQL查詢內部連接4表
我的表是低於
CREATE TABLE Members
(
MemberID nvarchar(50) NOT NULL PRIMARY KEY,
MemberName nvarchar(50) NOT NULL,
MemberAddress nvarchar(50),
MemberContact int,
MemberAge int,
RegistrationDate nvarchar(50)
);
--Inserting Values into Members' Table--
INSERT INTO Members(MemberID, MemberName, MemberAddress, MemberContact, MemberAge,RegistrationDate)
VALUES ('MEM01', 'Muhammed Abdul', 'Damansara','01121565987', '34', '20/02/2017');
CREATE TABLE Book
(
BookID nvarchar(50) NOT NULL PRIMARY KEY,
BookName nvarchar(50) NOT NULL,
BookCategory nvarchar(50),
BookPrice1 nvarchar(50),
Author nvarchar(50),
PublisherID nvarchar(50) NOT NULL FOREIGN KEY REFERENCES Publisher(PublisherID)
);
--Inserting Values into Book Table--
INSERT INTO Book(BookID, BookName, BookCategory, BookPrice1, Author, PublisherID)
VALUES ('B01', 'Harry Potter', 'Fantasy','70', 'Rowling J.K','PB01');
CREATE TABLE Orders_Bookstore
(
OrderID nvarchar(50) NOT NULL PRIMARY KEY,
BookID nvarchar(50) NOT NULL FOREIGN KEY REFERENCES Book(BookID),
OrderDate nvarchar(50),
OrderQuantity int,
OrderTotal int,
MemberID nvarchar(50) NOT NULL FOREIGN KEY REFERENCES Members(MemberID),
);
--Inserting Values into Boookstore Orders table--
INSERT INTO Orders_Bookstore(OrderID, BookID, OrderDate, OrderQuantity, OrderTotal, MemberID)
VALUES ('ORD1','B01', '04/06/2017', '3' ,'210', 'MEM01');
CREATE TABLE Receipt
(
ReceiptID nvarchar(50) NOT NULL PRIMARY KEY,
CartNum nvarchar(50) FOREIGN KEY REFERENCES ShoppingCart(CartNum),
DateOfDelivery nvarchar(50),
DeliveryStatus nvarchar(50),
MemberID nvarchar(50) FOREIGN KEY REFERENCES Members(MemberID)
);
--Inserting Values INTO Receipt table--
INSERT INTO Receipt(ReceiptID, CartNum, DateOfDelivery,DeliveryStatus)
VALUES ('REC1', 'CART1', '08/15/2017', 'Delivered');`
是我到目前爲止已經試過,但沒有運氣:
SELECT MemberID, MemberName, MemberAddress, MemberContact, BookID, BookName,
OrderQuantity, DeliveryStatus, DateOfDelivery FROM Members
INNER JOIN Orders_Bookstore ON Members.MemberID=Orders_Bookstore.MemberID
INNER JOIN Members ON Orders_Bookstore.MemberID=Members.MemberID
INNER JOIN Book ON Orders_Bookstore.BookID=Book.BookID
INNER JOIN Receipt ON Receipt.MemberID=Members.MemberID
任何幫助,將不勝感激
問題與您的查詢是什麼? – Jens
@Jens Msg 1013,Level 16,State 1,Line 17 FROM子句中的對象「成員」和「成員」具有相同的公開名稱。使用相關名稱來區分它們。 –
認爲你可以刪除INNER JOIN給成員,因爲這是基表。 –