简体   繁体   English

JOIN返回空集

[英]JOIN returns empty set

I need to show the names and IDs of all artists who have not recorded titles but do have web addresses listed in my database. 我需要显示所有未录制标题但在数据库中列出了网址的艺术家的姓名和ID。

This invovles two tables: 这将涉及两个表:

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

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

My query is as follows: 我的查询如下:

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

which returns an empty set. 返回一个空集。

Note that null is not a value so you cant mention it with equal or not equal symbol: Try this: 请注意,null不是值,因此您不能使用等号或不等号来提及它:尝试以下操作:

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

In MySql, null is not a value, so where ar.webaddress != NULL; 在MySql中,null不是值,因此where ar.webaddress != NULL; will not work. 不管用。 There is special syntax for checking nulls, is null and is not null for example. 检查空值有特殊的语法,例如, is nullis not null Also, the inner join will only give you artists that have titles. 此外,内部联接只会为您提供具有标题的艺术家。 To get artists without titles, try outer join and check for null in the joined table 要获得没有标题的艺术家,请尝试外部联接并在联接表中检查是否为空

ie

 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;
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

This will return records where there is a Webaddress but no title for a person 这将返回记录,其中有一个Webaddressno title

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM