像学生一样定义视图表,可防止对人表进行操作更新

时间:2018-09-13 07:20:02

标签: mysql sql

与人表有关的学生视图

UPDATE persons 
SET persons.EN_FAMILY = 'Test' 
WHERE persons.STCODE IN
        (SELECT students.PERSONS_STCODE AS STCODE 
         FROM students 
         WHERE @Counter > 0 
         AND students.ID NOT IN
            (SELECT sel_lesson.ST_ID 
             FROM sel_lesson 
             WHERE sel_lesson.YEAR = @CurrenYear 
             AND sel_lesson.TERM = @CurrentTerm 
             GROUP BY sel_lesson.ST_ID)
         AND students.STATUS IN('1','14')
);

1 个答案:

答案 0 :(得分:1)

尝试使用内部联接

UPDATE persons 
INNER JOIN  (SELECT students.PERSONS_STCODE AS STCODE 
         FROM students 
         WHERE @Counter > 0 
         AND students.ID NOT IN
            (SELECT sel_lesson.ST_ID 
             FROM sel_lesson 
             WHERE sel_lesson.YEAR = @CurrenYear 
             AND sel_lesson.TERM = @CurrentTerm 
             GROUP BY sel_lesson.ST_ID)
         AND students.STATUS IN('1','14') ) T on t.STCODE = persons.STCODE

SET persons.EN_FAMILY = 'Test'