2016-10-17 5 views
-1

나는 (이 알아낼 수있는 동안 나를 데려가는) 이것을 얻으려고 노력하고 있지만 작동하도록 업데이 트와 메신저 블랭킹 내포 된 쿼리가 필요해 보인다. 모든 팁/조언을 크게 주시면 감사하겠습니다.중첩 된 쿼리 업데이트

update `deal_change_log` 
set new_value = 'Paying' and old_value = 'Null' 
where id in (select item_id from deal_change_log 
      where field_key = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
      and `old_value` = 'billing details entered' 
      and `new_value` = 'paying' 
      and item_id not in (select item_id from `deal_change_log` d2 where d2.`field_key` = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
      and d2.`log_time` > `log_time`) 
order by item_id); 
+1

추가/태그 DBMS 서버 –

+0

주 일부 샘플 데이터, – Pirate

+0

는 보관하지 마십시오 NULL의 경우 NULL 텍스트, 즉'old_value = Null '을 수행합니다. – jarlh

답변

0

사용 별칭, 실행 속도가 느린 할 수 있기 때문에

update `deal_change_log` 
set new_value = 'Paying' and old_value = 'Null' 
where id in (select item_id from deal_change_log d1 
      where d1.field_key = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
      and d1.`old_value` = 'billing details entered' 
      and d1.`new_value` = 'paying' 
      and d1.item_id not in (select item_id from `deal_change_log` d2 where d2.`field_key` = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
      and d2.`log_time` > d1.`log_time`) 
order by d1.item_id); 
+0

아무 것도 작동하지 않습니다 - '대상 테이블을 지정할 수 없습니다.'deal_change_log 'FROM 절에서 업데이트하십시오.' MySQL 버전 5.6을 실행 중입니다. – b0uncyfr0

0

피가, 하위 쿼리를 사용

declare @item_id2 varchar(100) 
    select @item_id2 =item_id from `deal_change_log` d2 
      where d2.`field_key` = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
     and d2.`log_time` > `log_time`) order by item_id 
    declare @item_id varchar(100) 
    select @item_id= item_id from deal_change_log 
     where field_key = '458a901560dc06efb78fe365b951dacd3a6a1a11' 
     and `old_value` = 'billing details entered' 
     and `new_value` = 'paying' 
     and item_id not in (@item_id2) 

    update `deal_change_log` 
    set new_value = 'Paying' and old_value = 'Null' 
    where id in (@item_id) ;