【问题标题】:How to Update my column if there value exists in another table?如果另一个表中存在值,如何更新我的列?
【发布时间】:2020-01-29 09:59:56
【问题描述】:

我有两个表,班级和学生表。

student 表包含 student_name、class_time 和 class_id。

类表包含 class_time ,类 id 为 IsClassConduct。

如果学生表中存在时间和class_id,我想更新班级表的列IsClassConduct Y

和期望的结果将是

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 请添加文本数据而不是图像。

标签: sql oracle sql-update


【解决方案1】:
UPDATE ClassTable c
SET c.IsClassConduct = CASE WHEN EXIST ( SELECT 1
                                         FROM StudentTable s
                                         WHERE s.class_id = c.class_id
                                           AND INSTR(s.class_time, c.class_time) > 0 )
                            THEN 'Y'
                            ELSE 'N' 
                            END

?

【讨论】:

    【解决方案2】:
    update classTable set IsClassConduct = 'Y'
    where (Class_time,class_id) in (select class_time,class_id from student_talbe);
    

    或者你也可以使用内部连接来完成。

    【讨论】:

      【解决方案3】:

      您也可以使用 MERGE:

      merge into class A using (select class_id,class_time from student)B 
      on (A.class_id=B.class_id and instr(B.class_time,A.class_time)>0) 
      when matched then update set A.IsClassConduct='Y';
      

      您也可以使用 CONTAINS(B.class_time,A.class_time)>0 代替 INSTR,但为此您必须创建 INDEX。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多