programing

MySQL의 동일한 테이블을 참조하는 하위 쿼리를 사용한 SQL UPDATE

closeapi 2023. 9. 3. 16:17
반응형

MySQL의 동일한 테이블을 참조하는 하위 쿼리를 사용한 SQL UPDATE

UPDATE를 사용하여 테이블의 여러 행에 있는 열 값을 업데이트하려고 합니다.문제는 이 열에 대한 값을 도출하기 위해 하위 쿼리를 사용해야 하는데 동일한 테이블에 따라 다릅니다.질문은 다음과 같습니다.

UPDATE user_account student
SET student.student_education_facility_id = (
   SELECT teacher.education_facility_id
   FROM user_account teacher
   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'
)
WHERE student.user_type = 'ROLE_STUDENT';

보통 선생님과 학생이 두 개의 다른 테이블에 있다면 mysql은 불평하지 않을 것입니다.그러나 둘 다 같은 테이블을 사용하기 때문에 mysql은 대신 다음 오류를 발생시킵니다.

오류 1093(HY000):FROM 절에서 업데이트 대상 테이블 'student'를 지정할 수 없습니다.

mysql을 강제로 업데이트할 수 있는 방법이 있습니까?행이 업데이트됨에 따라 from 절이 영향을 받지 않을 것이라고 100% 확신합니다.

그렇지 않다면 이 업데이트 SQL을 작성하여 동일한 효과를 얻을 수 있는 다른 방법이 있습니까?

감사합니다!

편집: 작업을 시작한 것 같습니다.

UPDATE user_account student
LEFT JOIN user_account teacher ON teacher.user_account_id = student.teacher_id
SET student.student_education_facility_id = teacher.education_facility_id
WHERE student.user_type = 'ROLE_STUDENT';

당신을 위한 몇 가지 참고 자료 http://dev.mysql.com/doc/refman/5.0/en/update.html

UPDATE user_account student 
INNER JOIN user_account teacher ON
   teacher.user_account_id = student.teacher_id 
   AND teacher.user_type = 'ROLE_TEACHER'
SET student.student_education_facility_id = teacher.education_facility_id

테이블 및 열 이름이 명확한 추상 예제:

UPDATE tableName t1
INNER JOIN tableName t2 ON t2.ref_column = t1.ref_column
SET t1.column_to_update = t2.column_desired_value

@Nico가 제안한 대로

이것이 누군가에게 도움이 되기를 바랍니다.

UPDATE user_account 
SET (student_education_facility_id) = ( 
    SELECT teacher.education_facility_id
    FROM user_account teacher
    WHERE teacher.user_account_id = teacher_id
    AND teacher.user_type = 'ROLE_TEACHER'
)
WHERE user_type = 'ROLE_STUDENT'

위는 샘플 업데이트 쿼리입니다...

업데이트 SQL 문으로 하위 쿼리를 작성할 수 있으므로 해당 테이블의 별칭 이름을 지정할 필요가 없습니다.하위 쿼리 테이블에 별칭 이름을 지정합니다.노력해봤는데 잘 되고 있어요.

UPDATE user_account student

SET (student.student_education_facility_id) = (

   SELECT teacher.education_facility_id

   FROM user_account teacher

   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'

)

WHERE student.user_type = 'ROLE_STUDENT';

SQL Server에 필요했습니다.여기 있습니다.

UPDATE user_account 
SET student_education_facility_id = cnt.education_facility_id
from  (
   SELECT user_account_id,education_facility_id
   FROM user_account 
   WHERE user_type = 'ROLE_TEACHER'
) as cnt
WHERE user_account.user_type = 'ROLE_STUDENT' and cnt.user_account_id = user_account.teacher_id

다른 RDB와 함께 작동하는 것 같습니다.MSes(확인 부탁드립니다.저는 구문이 확장 가능하기 때문에 좋습니다.

제가 필요로 하는 형식은 다음과 같습니다.

UPDATE table1 
SET f1 = cnt.computed_column
from  (
   SELECT id,computed_column --can be any complex subquery
   FROM table1
) as cnt
WHERE cnt.id = table1.id

하위 쿼리에서 합계를 작성해야 했지만 솔루션이 하나도 작동하지 않았습니다.따라서 기본 테이블에 보기를 작성하고 이 보기에서 동일한 테이블에 액세스할 수 있습니다.

Create user_account_VW as select * from user_account

UPDATE user_account
SET user_account.student_education_facility_id =
    (SELECT user_account_VW.education_facility_id
     FROM user_account_VW
     WHERE user_account_VW.user_account_id = user_account.teacher_id
         AND user_account_VW.user_type = 'ROLE_TEACHER')
UPDATE user_account student, (
   SELECT teacher.education_facility_id as teacherid
   FROM user_account teacher
   WHERE teacher.user_account_id = student.teacher_id AND teacher.user_type = 'ROLE_TEACHER'
) teach SET student.student_education_facility_id= teach.teacherid WHERE student.user_type = 'ROLE_STUDENT';

언급URL : https://stackoverflow.com/questions/4268416/sql-update-with-sub-query-that-references-the-same-table-in-mysql

반응형