2012-10-31 61 views
1

我想寫一個Oracle查詢它會列出從repo1所有的ID,名稱&文件,該文件在file_repo匹配的ID,但沒有匹配的ID在repo1_status發現如何獲得與Oracle中的ID不匹配的行?

repo1 
id, name  
1, abc 
2, def 
3, emm 
4, xdd 

file_repo 
id, file 
1, a.c 
2, d.c 
3, e.c 
4, x.c 

repo1_status 
id, status, status_desc 
1, good, new generated 
3, good, new generated 

到目前爲止,我寫了下面的查詢,這不起作用。任何幫助

SELECT repo1.id, name,file 
FROM repo1,file_repo,repo1_status 
WHERE (repo1.id=file_repo.id and repo1.id!=repo1_status.id) 

回答

1

你可以試試這個

SELECT repo1.id, name,file_repo 
FROM repo1 INNER JOIN file_repo ON repo1.id = file_repo.id 
WHERE NOT EXISTS (SELECT id FROM repo1_status WHERE repo1_status.id = repo1.id) 

Here是鏈接大約EXISTS

OR

SELECT repo1.id, name,file_repo 
FROM ((repo1 INNER JOIN file_repo ON repo1.id = file_repo.id) 
     LEFT JOIN file_repo.id ON file_repo.id = repo1.id) 
WHERE file_repo.id is null 

一個left join這樣是一樣的NOT EXISTS和它可能有更好的表現

0

嘗試

SELECT repo1.id, name,file_repo 
FROM repo1 INNER JOIN file_repo ON repo1.id = file_repo.id 
WHERE repo1.id not in (SELECT id FROM repo1_status) 
相關問題