select count(*) from a,b where a.id=b.id and
a.name=b.name and a.xx=b.xx;update a set flag=1 where exists (select * from b where a.id=b.id and
a.name=b.name and a.xx=b.xx);
这俩语句处理的是相同的纪录吗? select 出来3w条纪录;update了才1w条纪录,为什么?应该怎么修改?

解决方案 »

  1.   

    楼主那样好像是不行吧,
    修改一下这样试试:
    update a set flag=1 where id in (select a.id from a,b where a.id=b.id and 
    a.name=b.name and a.xx=b.xx);
      

  2.   

    觉的楼主的Exists的写法才是对的.要理解这2种写法的区别,并且如果数据量大的话第一种的写法有更高的性能.
    1. select count(*) from a,b where a.id=b.id and a.name=b.name and a.xx=b.xx; 
    做的是笛卡尔集,就是说这个select的结果集里A表的数据有可能是重复的.2. where exists (select * from b where a.id=b.id and a.name=b.name and a.xx=b.xx); 
    这句只是要求更新A表在B表中存在的即可,和Select不一样,A不会重复.所以楼主原来的写法是正确的.
      

  3.   

    正如楼上所说,因为你的select 中,count 的 *,这是笛卡尔集,a.flag 是有重复的。
    下面语句的结果才和update 等价,其结果应该是1w 条。
    select count(distinct a.flag ) from a,b where a.id=b.id and
    a.name=b.name and a.xx=b.xx;
      

  4.   

    重复数据 可能 exists写法是对的 建议
    select count(*) from (select * from a,b where a.id=b.id and 
    a.name=b.name and a.xx=b.xx)看看有几条记录
      

  5.   

    楼上正确,但有个同志用in in处理的记录最多为1000条,所以不行
      

  6.   

    select 语句里面处理的是a 和b的数据。我举个例子给你看,你就知道为什么了
    a:表
    字段1  字段2    字段3
    1      2        3b:表
    字段1  字段2    字段3  字段4
    1      2        3      4
    1      2        3      5这样你查询出来就是
    1 2 3 1 2 3 4
    1 2 3 1 2 3 5
    有两条记录
    然后你的update语句,当然就是处理a表的那一条记录了
    明白了么?
      

  7.   

    15楼说的是正确的,你update只是一个表中符合条件的记录,而并不能update另一个表中的记录