我需要在SQL中进行查询才能显示畅销书。为此,我需要做一个内连接,并在我进行内连接后遇到问题。这是我的代码:
CREATE VIEW mostpopularbookssold AS
SELECT Count(orders.book_id) AS numberofbookssold ,
Top(1) books.[Name]
FROM orders
INNER JOIN books
ON books.id=orders.book_id
GROUP BY books.[Name]
select *
FROM mostpopularbookssold
答案 0 :(得分:1)
在TOP(1)
之后,SELECT
应放在查询的开头。此外,您必须通过出售的书籍进行订购:
CREATE VIEW MostPopularBooksSold
AS
SELECT TOP(1)
COUNT(Orders.Book_ID) AS NumberOfBooksSold
, Books.[Name]
FROM [the database name].[schema name].[Orders] AS Orders
INNER JOIN [the database name].[schema name].[Books] AS Books
ON Books.ID = Orders.Book_ID
GROUP BY Books.[Name]
ORDER BY COUNT(Orders.Book_ID) DESC
您应该使用数据库的名称替换数据库名称,您已在其中创建了相应的表,并且已将schema name
替换为您拥有的模式名称创建了相应的表(如果您没有明确说明模式名称,通常这是dbo。)