2

我在使用 SQL 语句时遇到问题。

基本上我有5张桌子,它们是:

书籍、记录、OrderedBooks、OrderedRecords 和 Orders。

My orders 包含客户下的所有订单,My orderedRecords 和 orderedBooks 包含与客户订单相关的所有账簿和记录,显然账簿和记录表包含与每本书和记录相关的信息。

我的orderedrecords 和orderedbooks 包含与相应书籍/记录相关的BookID 和RecordID。

我正在编写一个应用程序,它需要显示 booktitle、bookcost、bookQuantity、booksTotal(price*quantity) recordtitle、recordcost、recordQuantity、recordsTotal。

账簿和记录的总数不必使用 SQL 中的数学函数来计算。

到目前为止,我已经设法让它准确地给我想要的东西,但是,假设我有 3 个书籍订单和 1 个记录订单,对于记录订单的其余两行,它将复制数据而不是简单地空的。我尝试过左连接和右连接,但都没有用。

这是我的 SQL 语句...

SELECT Books.Title AS BookTitle
, Books.Cost AS BookCost
, OrderedBooks.Quantity AS BookQuantity
, OrderedBooks.Total AS BooksTotal
, Records.Title AS RecordsTitle
, Records.Cost AS RecordsCost
, OrderedRecords.Quantity AS RecordQuantity
, OrderedRecords.Total AS RecordsTotal 
-- Nothing wrong with select part
FROM Orders 
INNER JOIN OrderedBooks ON OrderedBooks.OrderID = Orders.ID 
FULL OUTER JOIN OrderedRecords ON OrderedRecords.OrderID = Orders.ID 
LEFT OUTER JOIN Books ON OrderedBooks.BookID = Books.ID 
LEFT OUTER JOIN Records ON OrderedRecords.RecordID = Records.ID 
WHERE (Orders.ID = 9)

它为正确的订单号返回正确的订购书籍和记录,但是我似乎无法摆脱重复的行

4

4 回答 4

5

图书编号:

SELECT Books.Title AS BookTitle
, Books.Cost AS BookCost
, OrderedBooks.Quantity AS BookQuantity
, OrderedBooks.Total AS BooksTotal
, Records.Title AS RecordsTitle
, Records.Cost AS RecordsCost
, OrderedRecords.Quantity AS RecordQuantity
, OrderedRecords.Total AS RecordsTotal
FROM Orders 
INNER JOIN OrderedBooks ON OrderedBooks.OrderID = Orders.ID 
FULL OUTER JOIN OrderedRecords ON OrderedRecords.OrderID = Orders.ID 
LEFT OUTER JOIN Books ON OrderedBooks.BookID = Books.ID 
LEFT OUTER JOIN Records ON OrderedRecords.RecordID = Records.ID 
WHERE (Orders.ID = 9)
GROUP BY Books.ID
于 2014-03-25T09:55:00.907 回答
1

一个简单的 SELECT DISTINCT 怎么样:

SELECT DISTINCT
Books.Title AS BookTitle
, Books.Cost AS BookCost
, OrderedBooks.Quantity AS BookQuantity
, OrderedBooks.Total AS BooksTotal
, Records.Title AS RecordsTitle
, Records.Cost AS RecordsCost
, OrderedRecords.Quantity AS RecordQuantity
, OrderedRecords.Total AS RecordsTotal 
-- Nothing wrong with select part
FROM Orders 
INNER JOIN OrderedBooks ON OrderedBooks.OrderID = Orders.ID 
FULL OUTER JOIN OrderedRecords ON OrderedRecords.OrderID = Orders.ID 
LEFT OUTER JOIN Books ON OrderedBooks.BookID = Books.ID 
LEFT OUTER JOIN Records ON OrderedRecords.RecordID = Records.ID 
WHERE (Orders.ID = 9)
于 2013-02-10T22:14:11.243 回答
0

尝试使用 GROUP BY 语句

像这样:

SELECT Books.Title AS BookTitle
, Books.Cost AS BookCost
, OrderedBooks.Quantity AS BookQuantity
, OrderedBooks.Total AS BooksTotal
, Records.Title AS RecordsTitle
, Records.Cost AS RecordsCost
, OrderedRecords.Quantity AS RecordQuantity
, OrderedRecords.Total AS RecordsTotal
FROM Orders 
INNER JOIN OrderedBooks ON OrderedBooks.OrderID = Orders.ID 
FULL OUTER JOIN OrderedRecords ON OrderedRecords.OrderID = Orders.ID 
LEFT OUTER JOIN Books ON OrderedBooks.BookID = Books.ID 
LEFT OUTER JOIN Records ON OrderedRecords.RecordID = Records.ID 
WHERE (Orders.ID = 9)
GROUP BY BookTitle
于 2013-02-11T01:45:56.633 回答
0

由于属于一个订单的账簿和记录之间没有关系,因此不应将它们连接在一起,而应使用两个不同的查询来查找账簿和记录的信息。

那么一种可能性是:

SELECT 'Book' AS Type
, Books.Title AS Title
, Books.Cost AS Cost
, OrderedBooks.Quantity AS Quantity
, OrderedBooks.Total AS Total
FROM Orders 
INNER JOIN OrderedBooks ON OrderedBooks.OrderID = Orders.ID 
INNER JOIN Books ON OrderedBooks.BookID = Books.ID 
WHERE (Orders.ID = 9)

UNION

SELECT 'Record' AS Type
, Records.Title AS Title
, Records.Cost AS Cost
, OrderedRecords.Quantity AS Quantity
, OrderedRecords.Total AS Total
FROM Orders 
INNER JOIN OrderedRecords ON OrderedRecords.OrderID = Orders.ID 
INNER JOIN Records ON OrderedRecords.RecordID = Records.ID 
WHERE (Orders.ID = 9)
于 2013-02-10T23:50:43.040 回答