MySQL:组函数的使用无效
我正在使用MySQL。这是我的架构:
供应商(sid:整数,sname:字符串,地址字符串)
零件(pid:整数,pname:字符串,颜色:字符串)
目录( >sid:整数,pid:整数,成本:实数)
(主键加粗)
我正在尝试编写一个查询来选择至少由两个供应商制造的所有零件:
-- Find the pids of parts supplied by at least two different suppliers.
SELECT c1.pid -- select the pid
FROM Catalog AS c1 -- from the Catalog table
WHERE c1.pid IN ( -- where that pid is in the set:
SELECT c2.pid -- of pids
FROM Catalog AS c2 -- from catalog
WHERE c2.pid = c1.pid AND COUNT(c2.sid) >= 2 -- where there are at least two corresponding sids
);
首先,我什至要去吗关于这个正确的方法是什么?
其次,我得到这个错误:
1111 - 群组功能使用无效
我做错了什么?
I am using MySQL. Here is my schema:
Suppliers(sid: integer, sname: string, address string)
Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)
(primary keys are bolded)
I am trying to write a query to select all parts that are made by at least two suppliers:
-- Find the pids of parts supplied by at least two different suppliers.
SELECT c1.pid -- select the pid
FROM Catalog AS c1 -- from the Catalog table
WHERE c1.pid IN ( -- where that pid is in the set:
SELECT c2.pid -- of pids
FROM Catalog AS c2 -- from catalog
WHERE c2.pid = c1.pid AND COUNT(c2.sid) >= 2 -- where there are at least two corresponding sids
);
First off, am I even going about this the right way?
Secondly, I get this error:
1111 - Invalid use of group function
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
HAVING
,而不是WHERE
。不同之处在于:
WHERE
子句过滤MySQL选择的行。 然后 MySQL 将行分组在一起并聚合COUNT
函数的数字。HAVING
类似于WHERE
,只是它发生在计算出COUNT
值之后,因此它的工作原理如下你期望的。将子查询重写为:You need to use
HAVING
, notWHERE
.The difference is: the
WHERE
clause filters which rows MySQL selects. Then MySQL groups the rows together and aggregates the numbers for yourCOUNT
function.HAVING
is likeWHERE
, only it happens after theCOUNT
value has been computed, so it'll work as you expect. Rewrite your subquery as:首先,您收到的错误是由于您使用
COUNT
函数的位置造成的 - 您不能在WHERE
中使用聚合(或组)函数条款。其次,不要使用子查询,只需将表连接到自身:
我认为应该只返回至少有两行具有相同 pid 的行,但至少有 2 个 sid 。为了确保每个
pid
仅返回一行,我应用了分组子句。First, the error you're getting is due to where you're using the
COUNT
function -- you can't use an aggregate (or group) function in theWHERE
clause.Second, instead of using a subquery, simply join the table to itself:
Which I believe should return only rows where at least two rows exist with the same
pid
but there is are at least 2sid
s. To make sure you get back only one row perpid
I've applied a grouping clause.如果您的 where 子句中没有聚合函数,
1111 - Invalid use of group function
错误的另一个可能来源是您有嵌套聚合函数:您可以通过分解来解决此问题将问题分为两个步骤:
If you don't have an aggregate function in your where clause, another possible source of the
1111 - Invalid use of group function
error is if you have nested aggregate functions:You can get around this by breaking up the problem into two steps: