MySQL:组函数的使用无效

发布于 2024-08-22 10:57:25 字数 843 浏览 7 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-08-29 10:57:25

您需要使用HAVING,而不是WHERE

不同之处在于:WHERE子句过滤MySQL选择的行。 然后 MySQL 将行分组在一起并聚合 COUNT 函数的数字。

HAVING 类似于 WHERE,只是它发生在计算出 COUNT 值之后,因此它的工作原理如下你期望的。将子查询重写为:

(                  -- where that pid is in the set:
SELECT c2.pid                  -- of pids
FROM Catalog AS c2             -- from catalog
WHERE c2.pid = c1.pid
HAVING COUNT(c2.sid) >= 2)

You need to use HAVING, not WHERE.

The difference is: the WHERE clause filters which rows MySQL selects. Then MySQL groups the rows together and aggregates the numbers for your COUNT function.

HAVING is like WHERE, only it happens after the COUNT value has been computed, so it'll work as you expect. Rewrite your subquery as:

(                  -- where that pid is in the set:
SELECT c2.pid                  -- of pids
FROM Catalog AS c2             -- from catalog
WHERE c2.pid = c1.pid
HAVING COUNT(c2.sid) >= 2)
单身情人 2024-08-29 10:57:25

首先,您收到的错误是由于您使用 COUNT 函数的位置造成的 - 您不能在 WHERE 中使用聚合(或组)函数条款。

其次,不要使用子查询,只需将表连接到自身:

SELECT a.pid 
FROM Catalog as a LEFT JOIN Catalog as b USING( pid )
WHERE a.sid != b.sid
GROUP BY a.pid

我认为应该只返回至少有两行具有相同 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 the WHERE clause.

Second, instead of using a subquery, simply join the table to itself:

SELECT a.pid 
FROM Catalog as a LEFT JOIN Catalog as b USING( pid )
WHERE a.sid != b.sid
GROUP BY a.pid

Which I believe should return only rows where at least two rows exist with the same pid but there is are at least 2 sids. To make sure you get back only one row per pid I've applied a grouping clause.

太傻旳人生 2024-08-29 10:57:25

如果您的 where 子句中没有聚合函数,1111 - Invalid use of group function 错误的另一个可能来源是您有嵌套聚合函数:

select sum(avg(close)) from prices;
(1111, 'Invalid use of group function')

您可以通过分解来解决此问题将问题分为两个步骤:

  1. 将内部聚合保存到变量中
select @avg:=avg(close) from prices;
  1. 对变量运行外部聚合
select sum(@avg) from prices;

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:

select sum(avg(close)) from prices;
(1111, 'Invalid use of group function')

You can get around this by breaking up the problem into two steps:

  1. Save the inner aggregation into a variable
select @avg:=avg(close) from prices;
  1. Run the outer aggregation against the variable
select sum(@avg) from prices;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文