2014-03-12 2 views
0

이 SELECT는 결과를 예상대로 반환합니다. 지금 쓰기에 대한 테이블을 잠글 필요가테이블 잠금 오류

SELECT 
    h.id, 
    h.rec_inv_num, 
    h.rec_inv, 
    h.trans_date, 
    h.trans_branch, 
    h.trans_type, 
    h.trans_number 
FROM 
    1_trans_history h 
WHERE 
    h.id IN (SELECT 
      h.id 
     FROM 
      1_trans_history_entries_stat s 
       LEFT JOIN 
      1_trans_history_entries e ON e.id = s.fk_trans_history_entries_id 
       LEFT JOIN 
      1_trans_history h ON h.id = e.fk_trans_history_id 
       LEFT JOIN 
      addresses a ON a.id = h.i_fk_addresses_id 
     WHERE 
      s.item_stat = 2 
     GROUP BY h.id 
     ORDER BY a.company , h.i_surname , h.i_middle_init , h.i_first_name , h.i_title); 

그래서이 추가하지만 "오류 코드 : 테이블 '1_trans_history가'LOCK 테이블이 고정되지 않은 1100"얻을

LOCK TABLES 
1_trans_history h WRITE, 
1_trans_history_entries e WRITE, 
1_trans_history_entries_stat s WRITE; 

나는 "이해 하나의 쿼리에서 여러 번 잠긴 테이블을 사용할 수는 없으며 대신 별칭을 사용하십시오.이 경우 별칭마다 별도로 잠금을 얻어야합니다. "

그래서 코드를 변경했지만 동일한 오류가 발생합니다

LOCK TABLES 
1_trans_history h WRITE, 
1_trans_history_entries e WRITE, 
1_trans_history_entries_stat s WRITE, 
1_trans_history h2 WRITE, 
1_trans_history_entries e2 WRITE, 
1_trans_history_entries_stat s2 WRITE, 
addresses a2 WRITE; 

SELECT 
    h.id, 
    h.rec_inv_num, 
    h.rec_inv, 
    h.trans_date, 
    h.trans_branch, 
    h.trans_type, 
    h.trans_number 
FROM 
    1_trans_history h 
WHERE 
    h.id IN (SELECT 
      h2.id 
     FROM 
      1_trans_history_entries_stat s2 
       LEFT JOIN 
      1_trans_history_entries e2 ON e2.id = s2.fk_trans_history_entries_id 
       LEFT JOIN 
      1_trans_history h2 ON h2.id = e2.fk_trans_history_id 
       LEFT JOIN 
      addresses a2 ON a2.id = h2.i_fk_addresses_id 
     WHERE 
      s2.item_stat = 2 
     GROUP BY h2.id 
     ORDER BY a2.company , h2.i_surname , h2.i_middle_init , h2.i_first_name , h2.i_title); 

unlock tables; 

답변

1

LOCK 문에 별칭과 혼동이 있다고 생각합니다. 1_trans_history 테이블의 별칭 중 하나를 제거하고 명시 적으로 LOCK 문에 추가했습니다. 또한 a2 및 s2 별칭의 이름을 좀 더 명료하게 변경했습니다.

LOCK TABLES 
1_trans_history WRITE, 
1_trans_history h WRITE, 
1_trans_history_entries e WRITE, 
1_trans_history_entries_stat s WRITE, 
addresses a WRITE; 

SELECT 
    id, 
    rec_inv_num, 
    rec_inv, 
    trans_date, 
    trans_branch, 
    trans_type, 
    trans_number 
FROM 
    1_trans_history 
WHERE 
    1_trans_history.id IN (SELECT 
     h.id 
    FROM 
     1_trans_history_entries_stat s 
      LEFT JOIN 
     1_trans_history_entries e ON e.id = s.fk_trans_history_entries_id 
      LEFT JOIN 
     1_trans_history h ON h.id = e.fk_trans_history_id 
      LEFT JOIN 
     addresses a ON a.id = h.i_fk_addresses_id 
    WHERE 
     s.item_stat = 2 
    GROUP BY h.id 
    ORDER BY a.company , h.i_surname , h.i_middle_init , h.i_first_name , h.i_title); 
UNLOCK TABLES; 

시도해보십시오.

솔직히 별칭이 필요한 유일한 테이블은 1_trans_history 뿐이지 만, 다른 별칭을 사용하면 쿼리가 더 읽기 쉽다고 생각합니다.