假设有: 表tb1 字段name id1,表tb2 字段name id2怎么样的sql可以比对出两个表相同的name值对应的id值不相同?

解决方案 »

  1.   


    SELECT  tb1.name, id1-id2 as sub from  tb1, tb2 where tb1.name = tb2.name
    sub = 0 就相同, != 0 就不相同如果 id1, id2 是字符串的话, 就要用 case when 了
      

  2.   

    select * from (
    select tb1.name,tb1.id1,tb2.id2 from tb1 join tb2 on tb1.id1=tb2.id2)tt1
    where id1<>id2
      

  3.   

    select a.*,b.* from tb1 a, tb2 b where a.`name` = b.`name` and a.id != b.id
      

  4.   

    select * from (
    select tb1.name,tb1.id1,tb2.id2 from tb1 join tb2 on tb1.id1=tb2.id2)tt1
    where id1<>id2  
    同意2