如何使用oracle比较两个列表值?

Select main.gr_number from 
(
Select st.GR_NUMber from student st where upper(st.class_id)=upper('jtm.online137') and st.is_active_flg='Y'
and st.status='STUDYING'
and upper(st.class_days) like  '%'||TO_CHAR(to_date('31-OCT-2019'),'DY')||'%'
) main 
where (Select GR_NUMber  from student_class_attend where upper(class_id)=upper('jtm.online137')
and attend_date ='31-OCT-2019') not in (main.GR_NUMber);

它给我错误

  

单行子查询返回多行

coo1cool798 回答:如何使用oracle比较两个列表值?

在我看来像NOT EXISTS,即

SELECT main.gr_number
  FROM (SELECT st.GR_NUMBER
          FROM student st
         WHERE     UPPER (st.class_id) = UPPER ('jtm.online137')
               AND st.is_active_flg = 'Y'
               AND st.status = 'STUDYING'
               AND UPPER (st.class_days) LIKE
                         '%'
                      || TO_CHAR (TO_DATE ('31-OCT-2019','dd-mon-yyyy'),'DY')
                      || '%') main
 WHERE NOT EXISTS
          (SELECT GR_NUMBER
             FROM student_class_attend
            WHERE     UPPER (class_id) = UPPER ('jtm.online137')
                  AND attend_date = TO_DATE ('31-OCT-2019','dd-mon-yyyy')
                  AND gr_number = main.GR_NUMBER);

请注意,我通过应用缺少的格式掩码和TO_DATE函数来修改了“日期”值,因为您不应该将日期与字符串进行比较。更好的是:使用日期文字,例如日期'2019-10-31'

,

您不能将WHERE与返回多行的子查询一起使用。
代替使用WHERE尝试使用左联接,并(对于NOT IN)检查左联接键中的空值:

Select main.gr_number 
from  (
    Select st.GR_NUMBER 
    from student st 
    where upper(st.class_id)=upper('jtm.online137') 
    and st.is_active_flg='Y'
    and st.status='STUDYING'
    and upper(st.class_days) like  '%'||TO_CHAR(to_date('31-OCT-2019'),'DY')||'%'
) main 
LEFT JOIN (
  Select GR_NUMBER  
  from student_class_attend 
  where upper(class_id)=upper('jtm.online137')
    and attend_date ='31-OCT-2019'
)  t ON  t.GR_NUMBER main.GR_NUMBER 
      AND t.GR_NUMBER is null;
本文链接:https://www.f2er.com/3166290.html

大家都在问