Mysql 用一个等效查询替换两个查询
这两个查询(工作正常)可以替换为一个吗Mysql 用一个等效查询替换两个查询,mysql,sql,sql-update,Mysql,Sql,Sql Update,这两个查询(工作正常)可以替换为一个吗 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
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列。从您的查询中,我了解到您正在更新多个
id
行,这些行的aggr
值相同。那么这一个应该有效:
update main
set aggr = group_concat(cascina_uk SEPARATOR '|')
group by uk
从您的查询中,我了解到您正在更新具有相同值
aggr
的多个id
行。那么这一个应该有效:
update main
set aggr = group_concat(cascina_uk SEPARATOR '|')
group by uk
试试这个
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)
试试这个
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)
使用MySQL的多表更新语法:
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
请注意,我只是将查询的部分重新排列为工作查询;我怀疑可以对子查询进行优化。使用MySQL的多表更新语法:
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
请注意,我只是将查询的部分重新排列为工作查询;我怀疑可以对子查询进行优化。不,您不能用一个等效查询替换插入和更新查询…第一个查询看起来像个坏主意。不,您不能用一个等效查询替换插入和更新查询…第一个查询看起来像个坏主意。