2016-11-15 43 views
0

比较。如果我在表1这样的数据同样列在同一个表

column1 column2 date 
111  00  2016-10-11 
111  00  2016-10-11 
111  20  2016-10-12 
111  20  2016-10-12 
222  00  2016-10-11 
222  20  2016-10-12 
333  20  2016-10-11 
333  20  2016-10-12 

我想建立其挑选出一个查询只333重复的,因为 列1 =列1(333 = 333),列2 =列2(20 = 20)和日期<>日期(2016年10月11日<> 2016年10月12日 谢谢

+0

其RDBMS,SQL服务器,ORCALE,MySQL的等? – Matt

+0

你能编辑你的请求吗?您的请求不够清楚。 – rtrigoso

回答

2

似乎自连接会工作......

SELECT T1.*, T2.* 
FROM Table1 T1 
INNER JOIN table2 T2 
on T1.Column1 = T2.Column1 
and T2.column2 = T2.Column2 
and T1.Date <> T2.Date 

或存在(速度较快,但只能访问T1数据)

SELECT T1.* 
FROM Table1 T1 
WHERE Exists (Select 1 
       from table1 T2 
       where T1.Column1 = T2.Column1 
       and T2.column2 = T2.Column2 
       and T1.Date <> T2.Date) 
1
select column1 from table1 t 
join table1 jt 
    on t.column1 = jt.column1 
    and t.column2 = jt.column2 
    and jt.date <> jt.date 
1

使用自加入

select t1.column1 
from my_table t1 
inner join my_table t2 
on t1.column1 = t2.column2 
and t1.ccolumn2 = t2.column2 
and t1.date <> t2.date