列 x 必须出现在 GROUP BY 子句中或在聚合函数中使用
Column x must appear in the GROUP BY clause or be used in an aggregate function
SELECT newTable.book_id
FROM (
SELECT book_id, SUM(stock) AS stock
FROM editions JOIN stock ON (editions.isbn = stock.isbn)
GROUP BY book_id
) AS newTable
HAVING stock = max(stock);
我希望此代码生成 book_id 库存最多的图书,即库存图书的最大份数。我收到以下错误:
ERROR: column "newtable.book_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: SELECT newTable.book_id
^
********** Error **********
ERROR: column "newtable.book_id" must appear in the GROUP BY clause or be used in an aggregate function
有什么解决办法吗?
PS。已经尝试在末尾添加 GROUP BY newtable.book_id, newtable.stock
。它产生了语法错误。
谢谢!
我认为您可以考虑如下编写查询。您只需按要聚合的列进行排序,这样最多 "in stock" 将位于顶部,然后使用 LIMIT 子句从顶部删除 X 项。在这种情况下,LIMIT 1.
SELECT book_id, SUM(stock) AS numstock
FROM editions JOIN stock ON (editions.isbn = stock.isbn)
GROUP BY book_id
ORDER BY numstock desc
LIMIT 1
SELECT newTable.book_id
FROM (
SELECT book_id, SUM(stock) AS stock
FROM editions JOIN stock ON (editions.isbn = stock.isbn)
GROUP BY book_id
) AS newTable
HAVING stock = max(stock);
我希望此代码生成 book_id 库存最多的图书,即库存图书的最大份数。我收到以下错误:
ERROR: column "newtable.book_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 2: SELECT newTable.book_id
^
********** Error **********
ERROR: column "newtable.book_id" must appear in the GROUP BY clause or be used in an aggregate function
有什么解决办法吗?
PS。已经尝试在末尾添加 GROUP BY newtable.book_id, newtable.stock
。它产生了语法错误。
谢谢!
我认为您可以考虑如下编写查询。您只需按要聚合的列进行排序,这样最多 "in stock" 将位于顶部,然后使用 LIMIT 子句从顶部删除 X 项。在这种情况下,LIMIT 1.
SELECT book_id, SUM(stock) AS numstock
FROM editions JOIN stock ON (editions.isbn = stock.isbn)
GROUP BY book_id
ORDER BY numstock desc
LIMIT 1