-1

我有两个表 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 

表2

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 来回答它,无论您喜欢什么。伙计们请帮帮我....thanx提前。

4

4 回答 4

0

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

于 2013-09-09T08:14:56.657 回答
0

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

Query = "Select * from table1 t1,table2 t2 where (YOUR CONDITION COMES HERE)"
于 2013-09-09T08:16:45.927 回答
0

我看着你的问题,想知道你为什么问,因为你自己制定了答案(至少是它的 where 子句)。

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)
于 2013-09-09T08:18:35.510 回答
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))
于 2013-09-09T08:21:34.177 回答