2013-10-27 105 views
0

我需要顯示所有未錄製標題的藝術家的名稱和ID,但是在我的數據庫中列出了Web地址。JOIN返回空集

這invovles兩個表:

Artists 
------- 
ArtistID, ArtistName, City, Region, WebAddress 

Titles 
------ 
TitleID, ArtistID, Title, StudioID, Genre 

我的查詢如下:

select ar.* 
from artists ar 
inner join titles t 
on ar.artistid = t.artistid 
where ar.webaddress != NULL; 

它返回一個空集。

回答

3

在MySQL中,null不是一個值,所以where ar.webaddress != NULL;將無法​​正常工作。例如,有特殊的語法來檢查空值,例如is nullis not null。此外,內部聯合只會給你有藝術家的頭銜。爲了讓藝術家沒有頭銜,嘗試外連接和連接表檢查空

select ar.* 
from artists ar 
left join titles t 
on ar.artistid = t.artistid 
where ar.webaddress Is not NULL 
and t.ArtistID is null; 
+1

這是唯一正確的答案 –

3

注意,null不是一個值,所以你不能用等於或不等於符號提到它: 試試這個:

select * 
from artists 
where ar.webaddress Is not NULL 
and artistid not in(select distinct artistid in titles) 
+0

曾獲Webaddressno title這仍然可以找到所有擁有錄製標題的藝術家嗎? – pcreech

+0

這將返回所有那些不爲null。 –

+0

看來,通過這個問題,cryptomath正在尋找沒有標題的藝術家,即不存在於標題表中 – pcreech

1
SELECT ar.* 
FROM Artists a INNER JOIN Titles T 
ON A.ArtistID = T..ArtistID 
WHERE a.WebAddress IS NOT NULL 
AND T.Title IS NULL 

這將返回記錄,其中有一個人