一个表有很多字段,下面有两条记录
1、select * from table where id = 1;
2、select * from table where id = 2;
现在我想比较两条记录有相同字段值的个数,除了循环比较外,有没有什么更高效的办法?

解决方案 »

  1.   

    没有了。
    select count
    =case when a.c1 = b.c1 then 1 else 0 end
    +...
    +case when a.cn = b.cn then 1 else 0 end
    from table a,table b 
    where a.id = 1 and  b.id = 2
      

  2.   

    select case when tb1.col1=tb2.col1 then 1 else 0 end +case when tb1.col2=tb2.col2 then 1 else 0 end +...
    from (select * from table where id = 1) cross join (select * from table where id = 2)
      

  3.   

    select case when tb1.col1=tb2.col1 then 1 else 0 end +case when tb1.col2=tb2.col2 then 1 else 0 end +... 
    from (select * from table where id = 1) as tb1 cross join (select * from table where id = 2) as tb2