我对SQL还很陌生,我想看看是否可以得到一些关于减法的帮助。我想减去studentstaken-studentsnotreviewed,然后将别名total分配给操作。我在想一些关于
select ((select count(*) from students where exams.id=students.exam_id) - (select count(*) from students where exams.id=students.exam_id and (students.review_flag='' or students.review_flag is null)) ) as 'Total'
但有些语法问题我不太确定。
select x.* from
(
select schools.name as School,
exams.name as name, exams.exam_start as examstart, exams.exam_end as examend,
(select count(*) from students where exams.id=students.exam_id) as studentstaken,
(select count(*) from students where exams.id=students.exam_id and (students.review_flag='' or students.review_flag is null)) as studentsnotreviewed,
#select () as 'Total'
case when exam_end< (now() - interval 2 day) then 'Yes' else 'No' end as 'Overdue',
exam_end + Interval 2 day as 'DueDate',
(select value from exam_options where exams.id=exam_options.exam_id and option_id=5) as IDtoggle,
(select value from exam_options where exams.id=exam_options.exam_id and option_id=6) as Roomscantoggle
from exams
left join taggables on exams.school_id=taggable_id
left join schools on exams.school_id=schools.id
where tag_id=12 and exam_start < now() and exam_start>'2021-01-01' and practice=0) as x
where studentsnotreviewed>0 and (studentsnotreviewed>15 or examend < now()) and (IDtoggle=1 or Roomscantoggle=1)
order by duedate asc, studentsnotreviewed desc
“样本数据”
[所需结果]
应该没问题。
select x.*
from
(
select schools.name as School,
exams.name as name, exams.exam_start as examstart, exams.exam_end as examend,
(select count(*) from students where exams.id=students.exam_id) as studentstaken,
(select count(*) from students where exams.id=students.exam_id and (students.review_flag='' or students.review_flag is null)) as studentsnotreviewed,
(select count(*) from students where exams.id=students.exam_id)
- (select count(*) from students where exams.id=students.exam_id and (students.review_flag='' or students.review_flag is null)) as 'Total',
... rest of the query