0

查询显示常用图书的图书编号、出版、价格和供应商名称。

上面的问题我使用了这个查询,但它返回错误。

select b1.book_code,b2.publication,b2.price,b3.supplier_name 
from BOOK_ISSUE b1 
join BOOK_DETAILS b2 on b1.BOOK_CODE=b2.BOOK_CODE 
join SUPPLIERS_DETAILS b3 on b2.SUPPLIER_ID=b3.SUPPLIER_ID 
group by b1.BOOK_CODE 
having COUNT(b1.book_code)>1

查询解释为:table1: book_issue table2: book_details table3: supplieer_details

上面的查询目的是想显示经常拿的书。我试过但有一些。

如何更正sql语句?

4

2 回答 2

0

我无法根据问题准确判断,但它可能很简单:

select b1.book_code,b2.publication,b2.price,b3.supplier_name 
from BOOK_ISSUE b1 
join BOOK_DETAILS b2 on b1.BOOK_CODE=b2.BOOK_CODE 
join SUPPLIERS_DETAILS b3 on b2.SUPPLIER_ID=b3.SUPPLIER_ID 
group by b1.book_code,b2.publication,b2.price,b3.supplier_name
having COUNT(*)>1
于 2013-06-09T04:29:43.627 回答
0

问题是您不能使用SELECT不属于GROUP BY没有聚合函数的子句中的列,因为 RDMBS 无法判断您要选择的组中的哪一行。

根据您想要的结果,有几种方法可以处理它:

  1. GROUP BY在子句中包含列
  2. 使用聚合函数MIN()MAX()...
  3. 使用子查询返回分组结果集,然后JOIN与其他表一起返回

如果没有看到表结构、示例数据和所需的输出,很难判断,但您可以尝试

SELECT b1.book_code,
       b2.publication,
       b2.price,
       b3.supplier_name 
  FROM
(
  SELECT book_code
    FROM book_issue
   GROUP BY book_code
  HAVING COUNT(*) > 1
) b1 JOIN book_details b2 
     ON b1.book_code = b2.book_code JOIN suppliers_details b3 
     ON b2.supplier_id = b3.supplier_id
于 2013-06-09T04:29:55.780 回答