it-source

테이블 'db_session'은 'DELETE'의 대상과 별도의 데이터 소스로 두 번 지정됩니다.

criticalcode 2022. 10. 30. 11:10
반응형

테이블 'db_session'은 'DELETE'의 대상과 별도의 데이터 소스로 두 번 지정됩니다.

이 SQL 쿼리를 실행하려고 하는데 위의 오류가 나타납니다.

DELETE FROM db_session
where time NOT IN (SELECT MAX(time) FROM db_session GROUP BY username)

SELECT정상적으로 동작합니다.

이 문제를 일으키는 유일한 데이터베이스는 MySQL(및 관련 데이터베이스)입니다.이 로직은 다음 로직으로 치환할 수 있습니다.JOIN:

delete s
    from db_session s join
         (select username, max(time) as maxtime
          from db_session s2
          group by username
         ) ss
         on s.username = ss.username and s.time < ss.maxtime;

추가적인 보너스는 논리도 정확하다는 것이다.로직은 시간이 최대 시간이 아닌 행을 삭제합니다. username다만, 다른 행이 다른 유저의 최대 시간이 되었을 경우에, 유저명에 대해서 복수의 행을 유지할 수 있는 경우.

언급URL : https://stackoverflow.com/questions/45287039/table-db-session-is-specified-twice-both-as-a-target-for-delete-and-as-a-se

반응형