2017-03-08 2 views
0

이 두 쿼리는 제대로 작동 할 수 있습니까?두 개의 쿼리를 하나의 동일한 쿼리로 대체하십시오.

INSERT INTO temptable 
(
    `id`,`aggr` 
) 
select a.id, b.aggr 
from main a 
inner join (
    select uk, group_concat(cascina_uk SEPARATOR '|') as aggr 
    from main 
    group by uk 
) b on a.uk = b.uk; 

update main, temptable set main.aggr = temptable.aggr where main.id=temptable.id; 

임시 테이블을 만들 필요가 없습니다. 필요한 것은 main.aggr 열을 업데이트하는 것입니다.

+0

아니요. 동일한 쿼리로 INSERT 및 UPDATE 쿼리를 바꿀 수 없습니다. –

+0

첫 번째 쿼리는 나쁜 아이디어처럼 보입니다. – Strawberry

답변

1

사용의 MySQL의 멀티 테이블 업데이트 구문을보십시오 작동중인 쿼리로; 하위 쿼리를 최적화 할 수있는 것으로 판단됩니다.

1

귀하의 문의에 따라 동일한 값이 aggr 인 복수 id 행을 업데이트하는 것으로 알고 있습니다. 그런 다음이 하나 작동합니다 : 만 이 쿼리의 부품을 재 배열이 그

update main, (
    select a.id, b.aggr 
    from main a 
    join (
     select uk, group_concat(cascina_uk SEPARATOR '|') as aggr 
     from main 
     group by uk 
    ) b on a.uk = b.uk 
) temptable 
set main.aggr = temptable.aggr 
where main.id = temptable.id 

참고 :

update main 
set aggr = group_concat(cascina_uk SEPARATOR '|') 
group by uk 
1

update main c 
set 
    c.aggr = (select b.aggr 
       from 
        main a, 
        (select uk, group_concat(cascina_uk SEPARATOR '|') as aggr from main group by uk) b 
       where 
       a.uk = b.uk 
       and c.id = a.id)