我有一个关系模式如下:
INVENTORY(inventoryID,title,wholesale,markup,qtyinStock,discount)
Primary-Key: inventoryID
BOOK(coverType,inventoryID)
Primary-Key: inventoryID
Foreign-Key: inventoryID
DVD(classification,ReleaseYear,StarRating)
Primary-Key: inventoryID
Foreign-Key: inventoryID
BOOK和DVD基本上是库存的子类型。他们继承了INVENTORY。现在,我想将库存中的所有项目显示为书籍或DVD,具体取决于表格中的项目。我做了以下但我不确定这是否正确:
SELECT title FROM INVENTORY,BOOK,DVD
WHERE INVENTORY.inventoryID = BOOK.inventoryID AND INVENTORY.inventoryID = DVD.inventoryID
但这并不显示项目的标题。我该如何添加?谢谢!
答案 0 :(得分:1)
除非我错误地阅读您的问题,否则查询很简单。您的示例查询会提取同时包含DVD和Book的库存。
SELECT title,
CASE WHEN b.inventoryID IS NOT NULL THEN 'Book'
WHEN d.inventoryID IS NOT NULL THEN 'DVD'
END AS Category
FROM Inventory i
JOIN Book b on i.inventoryID = b.inventoryID
JOIN DVD d on d.inventoryID = i.InventoryID
答案 1 :(得分:0)
使用WHERE子句中的AND
,您将尝试获取同时为book AND dvd的库存项目的标题。尝试更改AND
和OR