我需要table1和table 2中包含重复数据的所有列

时间:2013-09-09 08:07:12

标签: java mysql

我有两个表table1和table2
例如:表1

id1     name1    fatherName1    village1    category1    subcategory1  
1,        a,       x1,          v1,          c1,           sc1   
2,        b,       x2,          v2,          c2,           sc2   
3,        a,       x1,          v1,          c3,           sc3  
4,        c,       x4,          v4,          c4,            sc4 

table2

id2     name2   fatherName2  village2  category2   subcategory2  
1,        a,      x1,            v1,        c5,        sc5   
2,        b,      x2,            v2,        c2,        sc2  
3,        c,      x5,            v5,        c3,        sc3  
4,        d,      x6,           v6,         c6,        sc6  

上面我提到了6列和4行表 现在,我需要

all the rows table1 and table2 where 
(table1.name1=table2.name2 
and table1.fatherName1= table2.fateherName2 
and table1.village1=table2.village2) 
OR (table1.name1=table1.name1 
and table1.fatherName1= table1.fateherName1 
and table1.village1=table1.village1) 

您可以通过java或sql来回答它。伙计们请提前帮助我....比如。

4 个答案:

答案 0 :(得分:0)

我猜你可以使用'union'和'distinct'来做你想做的事情:)

答案 1 :(得分:0)

参考this。除此之外,我会给你一个部分答案: 您的查询应该是这样的:

Query = "Select * from table1 t1,table2 t2 where (YOUR CONDITION COMES HERE)"

答案 2 :(得分:0)

我看着你的问题并想知道你为什么要问,因为你有自己的答案(至少是它的where-clause)。

select *
from   table1, table2
where (table1.name1=table2.name2 
and table1.fatherName1 = table2.fatherName2
and table1.village1=table2.village2
) OR 
(table1.name1=table1.name1 
and table1.fatherName1= table1.fatherName1 
and table1.village1=table1.village1)

答案 3 :(得分:0)

      (select t1.id1 as id,t1.name1 as name from table1 t1 inner join t2 on (t1.name1=t2.name2 and t1.fatherName1=t2.fatherName2 and t1.village1=t2.village2))
        union all
      (select t2.id2 as id,t2.name2 as name from table2 t2 inner join t1 on (t1.name1=t2.name2 and t1.fatherName1=t2.fatherName2 and t1.village1=t2.village2))