简体   繁体   English

使用时执行查询时出错

[英]Error while executing a query while using having

This the query that we are trying to execute 这是我们正在尝试执行的查询

select
     *
from pub_int_marks
where pub_int_marks.is_active = 1
having count(select
                  student.id
               from student
              inner join prev_class_data on prev_class_data.student_id = student.id
              inner join class_scheme on student.class_scheme_id = class_scheme.id
              where (prev_class_data.class_id = pub_int_marks.class_id or class_scheme.class_id = pub_int_marks.class_id)
              and student.id = 15) > 0

Both the inner query and the outer query works fine independently, but when combined its giving error. 内部查询和外部查询都可以独立正常运行,但是结合使用时会产生错误。 What is wrong with the query? 查询出了什么问题?

I'm not sure what exactly your query is supposed to be doing, but I don't think the subquery with a HAVING is the way to do it. 我不确定您的查询到底应该做什么,但是我不认为带有HAVING的子查询就是这样做的方法。

What about something like this? 那这样的东西呢?

select pub_int_marks.*
from student
inner join prev_class_data 
    on prev_class_data.student_id = student.id
inner join class_scheme 
    on student.class_scheme_id = class_scheme.id
inner join pub_int_marks
    on prev_class_data.class_id = pub_int_marks.class_id or class_scheme.class_id = pub_int_marks.class_id
where student.id = 15

or if you don't want to use an OR in a JOIN ON: 或者如果您不想在JOIN ON中使用OR:

select pub_int_marks.*
from student
inner join class_scheme 
    on student.class_scheme_id = class_scheme.id
inner join pub_int_marks
    on class_scheme.class_id = pub_int_marks.class_id
where student.id = 15

union

select pub_int_marks.*
from student
inner join prev_class_data 
    on prev_class_data.student_id = student.id
inner join pub_int_marks
    on prev_class_data.class_id = pub_int_marks.class_id
where student.id = 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM