【问题标题】:Having issues solving the error operand should contain 1 column解决错误操作数时遇到问题应包含 1 列
【发布时间】:2019-07-28 18:18:10
【问题描述】:

我正在尝试选择班级中失败学生最多的老师的姓名。

select 
    Failed.section, max(Failed) as Failed_Class 
from 
    (select 
         e.section, count(e.grade) * 100 / (select count(*)
                                            from enrollment) as Failed
     from 
         enrollment e 
     where 
         e.grade = 'D' or e.grade = 'F'
     group by 
         e.section) as Failed;

这将返回部分 id 和该部分中不及格学生的百分比。

select 
    p.name
from 
    professors p, teaches t 
where 
    p.ssn = t.ssn 
    and t.section in (select Failed.section, max(Failed) as Failed_Class 
                      from 
                          (select e.section, count(e.grade) * 100 / (select count(*) from enrollment) as Failed
                           from enrollment e 
                           where e.grade = 'D' or e.grade = 'F'
                           group by e.section) as Failed);

当我执行 sub select 语句时,我得到了错误,我不知道如何解决这个问题?

我得到的错误是:

操作数应包含 1 列

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。并且从不FROM 子句中使用逗号。
  • 您使用的是哪个DBMS 产品? “SQL”只是一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加标签
  • 我正在使用 mySQL

标签: sql max operands


【解决方案1】:

该错误与您使用返回两列并尝试与一列的 in 子句匹配的子查询有关。

另外,如果您只需要一位老师,您可以使用 order by max value desclimit 1

select p.name
from professors p 
inner join teaches t ON p.ssn = t.ssn 
  and t.section = t.section in (
  select Failed.section
  from (
    select e.section 
    ,count(e.grade)*100/ ( select count(*)  from enrollment ) as Failed
  from enrollment e 
  where e.grade = 'D' or e.grade = 'F'
  group by e.section
  ORDER BY failed 
  limit 1 
  ) t 
);

最后,您应该使用显式连接语法(而不是旧的基于位置的隐式连接语法)。

【讨论】:

  • 这会导致错误'Failed' not supported(referenced to group function)
猜你喜欢
  • 2014-11-07
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 2013-03-27
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多