嘿所有,我正在艰难地写下这个问题,这就是我在网上找不到答案的原因,所以我能做的最好的就是举个例子。我有以下数据库表:
ACTORNAME SERIESNAME
------------------------------ ------------
baldwin found
baldwin lost
baldwin rocks
baldwin sienfield
costelo friends
costelo prince
costelo remember
denzel friends
denzel prince
denzel remember
fox found
fox friends
fox prince
lopez found
lopez friends
lopez prince
lopez remember
pitt er
pitt everybody
pitt friends
pitt heroes
pitt rocks
smith friends
smith prince
smith remember
我想使用一个SELECT语句来获取在史密斯播放的所有相同系列中播放的actor名称。所以生成的actor名应该是:
costelo,denzel和lopez我甚至不确定要使用哪个关键字。我正在看JOIN命令并且还尝试了MINUS,我能得到的最接近的是演员姓名,这些演员名称与史密斯演奏的系列完全相同(在那种情况下,lopez不包括在内并且是错误的)
这是另一种解释:
suppose Smith acts in movies X and Y.
Suppose also that actor
A acts in movies X, Z
B acts in Y
C acts in X, Y, Z
D acts in X, Y
The answer to the query should be actors C and D.
换句话说,你必须归还那些电影中包含演员史密斯的演员。
寻找正确方向, 托梅克
答案 0 :(得分:2)
对不起。我原来的答案误解了你的意图。试试这个:
select t2.actorname,
count(t2.seriesname)
from mytable t1
join mytable t2
on t1.seriesname=t2.seriesname and t1.actorname='smith' and t2.actorname <> 'smith' group by t2.actorname
having count(t2.seriesname)=(select count(seriesname) from mytable where actorname='smith')
答案 1 :(得分:0)
SELECT DISTINCT ActorName
FROM dbo.MyTable
WHERE SeriesName IN (SELECT SeriesName FROM dbo.MyTable WHERE ActorName = 'smith');