1

考虑 BOOKSTORE 数据库的以下数据库模式:

Books (bookid, title, author, year)
Customers (customerid, name, email)
Purchases (customerid, bookid, year)
Reviews (customerid, bookid, rating)
Pricing (bookid, format, price)

我有这个问题:

查找有关“CIVIL WAR”(即标题包含“CIVIL WAR”)的书籍的评分信息(显示标题、作者和平均评分)。

到目前为止,我得到的是:

SELECT title, author, AVG(r1.rating)
FROM Books, Reviews
WHERE title LIKE '%CIVIL WAR%'

但它是错误的:(任何帮助将不胜感激:D

4

2 回答 2

3

这遵循您提供的结构:

SELECT books.title, books.author, AVG(reviews.rating)
  FROM books, reviews
 WHERE books.bookid = reviews.bookid 
   AND books.title LIKE '%CIVIL WAR%'
GROUP BY books.title, books.author

您还可以考虑使用 a而不是像我那样JOIN在子句中进行 join :WHERE

FROM books
JOIN reviews ON reviews.bookid = books.bookid

此外,请考虑为表使用别名,因为这将缩短更复杂的查询:

FROM `Books b` 

--Will enable you to write 
 AND b.title LIKE '%CIVIL WAR%'
于 2013-10-11T12:10:51.233 回答
2

尝试这个:

SELECT B.title, B.author, AVG(R.rating) Avg_Rating
FROM Books B
  JOIN Reviews R
   ON B.bookid = R.bookid
WHERE B.title LIKE '%CIVIL WAR%'
GROUP BY B.title, B.author

如果您有多个标题/作者,您也可以按 bookid 分组。

SELECT B.title, B.author, AVG(R.rating) Avg_Rating
FROM Books B
  JOIN Reviews R
   ON B.bookid = R.bookid
WHERE B.title LIKE '%CIVIL WAR%'
GROUP BY B.bookid, B.title, B.author
于 2013-10-11T12:12:49.887 回答