那你倒底是想更新为b.bf1还是c.cf1?还是有什么需求?

解决方案 »

  1.   

    具体意思是这样的
    我要更新的a1的新内容来自于一个查询(括号内),但是更新的时候a1需要和这个查询进行匹配,利用一个关键字段进行匹配,但是这个关键字并不是需要更新的字段。
    我曾经这样写:
    update a1
    set a1.f1=(
    select count(b1.f2)
    from
    b1,c1
    where b1.f1=c1.f1)
    where a1.pk1=b1.pk1
    错误提示为b1.pk1是无法识别的字段
      

  2.   

    改成
    update a1
    set a1.f1=(
    select count(b1.f2)
    from
    b1,c1
    where b1.f1=c1.f1)
    where exists ( select 1 from b1 where a1.pk1=b1.pk1)试试,能不能达到你的要求。不过按照上面的SQL,你的a1中所有满足后一个where条件的记录的f1都是相同的值,这是你要的吗?也许下面的更符合你的要求。
    update a1
    set a1.f1=(
    select count(b1.f2)
    from
    b1,c1
    where b1.f1=c1.f1 and a1.pk1=b1.pk1 )
    where exists ( select 1 from b1 where a1.pk1=b1.pk1)where exists ( select 1 from b1 where a1.pk1=b1.pk1)只是限制更新的记录,有些应该是不需要更新的?
      

  3.   

    这样好像可以,没有错误,可以执行,但是结果集还需要看一下是不是一样。应该没有问题where exists ( select 1 from b1 where a1.pk1=b1.pk1)具体是什么意思呢?
      

  4.   

    update a1
    set a1.f1=(
    select (select count(1) from c1 where c1.f1=b1.f1) from b1 where b1.pk1=a1.pk1)