【问题标题】:Why does this MySQL statement not work when inside a subquery?为什么这个 MySQL 语句在子查询中不起作用?
【发布时间】:2014-03-27 07:57:59
【问题描述】:

为什么这个语句是自己工作的,但是当它是子查询的一部分时,它说无效使用组函数

select count(cid) 
  from qualification q 
       inner join faculty f 
           on q.fid=f.fid 
           where fname='Berry'
group by 
       f.fid;

如何修改它以适应子查询?

整个查询 -

select fid, fname from faculty
where fid in
(select fid from qualification where count(cid)=
    (select count(cid) from qualification q inner join faculty f on
    q.fid=f.fid where fname='Berry' group by f.fid));

Logic : 列出所有可以教授 Berry 教授可以教授的所有课程的教员的 fname 和 fid

【问题讨论】:

  • 你能显示整个查询吗?
  • 聚合函数过滤器必须与HAVING一起使用,而不是WHERE

标签: mysql sql


【解决方案1】:

对我来说,问题是在WHERE 中使用count()

where count(cid)=
(select count(cid) from qualification q inner join faculty f on
q.fid=f.fid where fname='Berry' group by f.fid)

你可以试试改成这样处理

HAVING count(cid)=
(select count(cid) from qualification q inner join faculty f on
q.fid=f.fid where fname='Berry' group by f.fid)

但我不明白其中的逻辑。如果您解释得更多,我们会建议更好的解决方案

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2012-02-11
    • 2016-07-16
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多