2009-10-22 34 views
0

嘿所有,我有一个困难的时候措辞这正确,这就是为什么我无法在网上找到答案,所以最好的我可以做的就是举一个例子。我有以下的数据库表:SQL比较一列信息集

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 

而且我想用这将抢在所有史密斯扮演同一系列的发挥actornames一个SELECT语句,因此所产生的actornames应该是:

costelo,denzel和lopez

我甚至不确定要使用哪个关键字。我正在查看JOIN命令,并尝试了MINUS,我可以得到最接近的演员姓名,这些演员姓名完全在smith播放的同一系列中播放(在这种情况下,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. 

换句话说,你必须回到那些演员,其集电影包含这些演员史密斯。

寻找在正确的方向一推, 托梅克

回答

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') 
+0

这将返回所有的,演员是在任何史密斯扮演的系列产品。我需要在史密斯玩过的所有系列中都演过的演员。 – Tomek

+0

没有这个错......这不是什么意图。它会给出额外的行 –

+0

这个声明只返回只玩过这个系列赛史密斯已经参加过并且不包括那些参加了所有系列赛史密斯演出的演员,这些都应该包含在答案中。 (我在原来的问题中添加了另一个解释,以便更清楚一些事情,就像我说的这很难正确说出) – Tomek

0
SELECT DISTINCT ActorName 
    FROM dbo.MyTable 
    WHERE SeriesName IN (SELECT SeriesName FROM dbo.MyTable WHERE ActorName = 'smith');