SQL 其中参数返回为数字而非布尔值

SQL Where argument returning as numeric and not boolean

我是 sql 的新手,我一直遇到错误 "argument of WHERE must be type boolean, not type numeric" 来自以下代码

select subject_no, subject_name, class_size from Subject
where (select AVG(mark) from Grades where mark > 75)
group by subject_no
order by subject_no asc

为了帮助理解这个问题,我试图做的是列出平均分低于 75 分的科目

根据我的理解,虽然 where 参数将是布尔值,因为 class 的平均分数将高于或低于 60,因此是真还是假,非常感谢任何帮助纠正我的理解。

您的查询中有更多错误 - 您必须将成绩 table 加入主题 Table,将条件移到 select 语句之外(并更改 ><),最后删除 group by 子句:

select subject_no, subject_name, class_size from Subject
where (select AVG(mark) from Grades where Grades.subject_no = Subject.subject_no) < 75
order by subject_no asc

您应该将您的 select 语句重写为这种优化形式(另外您在结果集中获得平均分数):

select subject_no, subject_name, class_size, avg_mark
from Subject s
join (select subject_no, AVG(mark) avg_mark
      from Grades
      group by subject_no
      having AVG(mark) < 75) g on g.subject_no = s.subject_no
order by subject_no asc

已编辑!

使用相关子查询查找 avg(grade) < 75 的主题。不需要 GROUP BY 因为没有聚合函数,请使用 DISTINCT 来删除重复项:

select distinct subject_no, subject_name, class_size
from Subject s
where (select AVG(mark) from grades g
       where g.subject_no = s.subject_no) < 75
order by subject_no asc

请注意,我假设 Grades table 中也有 subject_no 列。

首先,(select AVG(mark) from Grades where mark > 75) 的 Return 值不是您提到的布尔值。它正是 AVG(mark) 本身。所以你实际上可以这样写:

select 1+1 from dual and the return value is 2 or select 'hello world ' from dual 

和 return 值正是 String hello world。

因此,如果您想要列出平均分低于 75 分的科目。以下关于哪里的陈述应该更像是:

mark<(select AVG(mark) from Grades where mark > 75)

这将 return 一个布尔值。 但是,您解释问题的陈述太难理解了:P 我想程序员需要多一点时间来理解 SQL 而你不是 一开始太熟悉了。祝你好运。如果您能更准确地解释您的问题...更容易找到您正在寻找的正确答案。