2016-11-15 30 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