2013-03-07 31 views
0

我做從表1,其記錄的計數/行不表2中Sybase的SELECT COUNT「不舒正確,嘗試比較兩個表

存在這裏是查詢:

select count(1) from table1 
where not exists (select 1 from table2 where 
        table1.col1 = table2.col1 
        and table2.id=1) 

我需要查看table2中缺少的記錄,其table2 = 1中的id,並且這些記錄應該在table1中可用。這裏的PK是col1。

該查詢返回我0.但是,如果我做一個Excel工作表比較通過刪除兩個表到Excel。我可以找到table1中缺少的1591條記錄,並且可以在table2中找到。 這裏有什麼可能是錯的?

+0

請問這是[標籤:sybase-asa],[標籤:sybase-ase],[標籤:sybase-iq]還是[標籤:sqlanywhere]?試圖清理[tag:sybase]標記... – 2013-03-15 07:55:28

回答

0

您的查詢工作正常。

您查詢發現,存在於table1但不是在table2

你只能用不table1 EXISTS Excel中找到記錄和table2

EXISTS如果你想找到這些記錄與記錄SQL比你的查詢應該是:

select count(1) from table2 
where table2.id=1 and table2.col1 not in (select col1 from table1) 

或不存在此查詢的版本:

select count(1) from table2 
where table2.id=1 and 
     not exists (select 1 from table1 where table1.col1=table2.col1) 

我沒有測試查詢。