两个表联合查询出来的东西,怎样更新回其中的一个表?
比如说下面的语句:
SELECT table1.word , SUM(table2.Pxx*table2.Pyx) AS Expr1
FROM table1 INNER JOIN table2
ON table1.word = table2.S1 OR table1.word = table2.S2 
GROUP BY table1.word
现在想把这个结果放回table1对应的字段sums处,即set sums=expr1
具体的语句该怎么加进去?

解决方案 »

  1.   

    update table1 set sums=(SELECT table1.word , SUM(table2.Pxx*table2.Pyx) AS Expr1
    FROM table1 INNER JOIN table2
    ON table1.word = table2.S1 OR table1.word = table2.S2 
    GROUP BY table1.word
    )
      

  2.   

    update table1 set sums=(SELECT table1.word , SUM(table2.Pxx*table2.Pyx) AS Expr1
    FROM table1 INNER JOIN table2
    ON table1.word = table2.S1 OR table1.word = table2.S2 
    GROUP BY table1.word
    )
      

  3.   

    不行啊,你的办法:
    update table1 set sums=(SELECT table1.word , SUM(table2.Pxx*table2.Pyx) AS Expr1
    FROM ……
    报错说:type mismatch in expression
    报错又说:single row subquery produced more than one row
      

  4.   

    去掉table1.word .
    update table1 set sums=(SELECT SUM(table2.Pxx*table2.Pyx) AS Expr1
    FROM table1 INNER JOIN table2
    ON table1.word = table2.S1 OR table1.word = table2.S2 
    GROUP BY table1.word
    )
      

  5.   

    update table1 set sums=(SUM(table2.Pxx*table2.Pyx) AS Expr1
    FROM table1 INNER JOIN table2
    ON table1.word = table2.S1 OR table1.word = table2.S2 
    GROUP BY table1.word
    )
      

  6.   

    还是报这个错阿:single row subquery produced more than one row
      

  7.   

    Create table table1(word char(10),sums  float)
    go
    insert table1 values('aaa',80)
    insert table1 values('bbb',86)
    insert table1 values('ccc',75)
    create table  table2(s1 char(10),s2 char(10),pxx  float,pyx float)
    go  
    insert table2 values('ddd','aaa',3,4)
    insert table2 values('aaa','ccc',1,2)
    --以下为更新语句
    update  table1
         set  sums=(
    SELECT  SUM(table2.Pxx*table2.Pyx) 
    FROM table1 INNER JOIN table2 ON table1.word = table2.S1 OR table1.word = table2.S2
    GROUP BY table1.word
    having  table1.word=a.word)
         from  table1   a  drop table table1
    drop table table 
    --测试结果
    --word  sums 
    --aaa   14  
    --bbb   null
    --ccc   2
    操作系统win2000,数据库sql server2000下测试成功
      

  8.   

    忘了提醒大家注意的是:我用的是tquery控件,.db数据库,好多在sql server下正确运行的语句在这都不支持的
      

  9.   

    UPDATE table1
    SET sums=(select sums from table2
    WHERE table1.s1 = table2.Ss)