Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 用一个等效查询替换两个查询_Mysql_Sql_Sql Update - Fatal编程技术网

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

请注意,我只是将查询的部分重新排列为工作查询;我怀疑可以对子查询进行优化。

不,您不能用一个等效查询替换插入和更新查询…第一个查询看起来像个坏主意。不,您不能用一个等效查询替换插入和更新查询…第一个查询看起来像个坏主意。