根据一列

时间:2017-09-13 08:36:59

标签: sql sql-server tsql sql-server-2016

示例数据集:

enter image description here

我有3个不同的列

  1. FilmAdi(FilmName),
  2. OyuncuAdi(ActorName)
  3. OyuncuSoyadı(ActorSurname)
  4. 如果用户输入两个不同的演员姓名,例如

    OyuncuAdi='Şener', OyuncuSoyadı='Şen' and OyuncuAdi='Kemal',OyuncuSurname='Sunal' 
    

    如果有任何一起播放的电影,请获取电影名称。 样本输出如下。

    'Tosun Paşa'
    

    编辑:我经过太多尝试后解决了问题,这个查询是解决方案,这是我的想法

    select f.FilmAdi
    from Oyuncular o 
    join FilmOyuncular fo on fo.OyuncuId=o.OyuncuId
    join Filmler f on f.FilmId=fo.FilmId
    where o.OyuncuId in('4','5') --Oyuncu.OyuncuID 4 ve 5 olan oyuncuların oynadıkları filmler
    group by f.FilmAdi
    having COUNT(f.FilmId)>1
    

3 个答案:

答案 0 :(得分:2)

试试这个:

declare @filmactors table
(
film varchar(20),
firstname varchar(20),
surname varchar(20)
)

INSERT INTO @filmactors VALUES
('Tosun Pasa', 'Kemal', 'Sunal'),
('Tosun Pasa', 'Sener', 'Sen'),
('Av Mevsimi', 'Sener', 'Sen')

declare @firstActorName varchar(20) = 'Kemal'
declare @firstActorSurName varchar(20) = 'Sunal'
declare @secondActorName varchar(20) = 'Sener'
declare @secondActorSurName varchar(20) = 'Sen'


SELECT f1.film FROM @filmactors f1
INNER JOIN @filmactors f2 ON f1.film = f2.film
WHERE 
    f1.firstname = @firstActorName AND
    f1.surname = @firstActorSurName
AND f2.firstname = @secondActorName AND
    f2.surname = @secondActorSurName

结果:

Tosun Pasa

这使用自联接。基本上它选择所有电影与第一个演员和所有电影与第二演员并加入他们在电影是相同的。

答案 1 :(得分:1)

假设您的表名为[FilmActor],您可以执行以下操作

SELECT FA1.[FilmAdi]
FROM [FilmActor] FA1
INNER JOIN [FilmActor] FA2
ON FA2.[FilmAdi] = FA1.[FilmAdi]
AND FA2.[OyuncuAdi] = 'Kemal'
AND FA2.[OyuncuSurname] = 'Sunal'
WHERE
FA1.[OyuncuAdi]='Şener'
AND FA1.[OyuncuSoyadı] = 'Şen'
;

答案 2 :(得分:0)

这将为您提供所有包含多个演员的电影。

SELECT DISTINCT FilmName from (
SELECT [FilmName]
  ,[ActorName]
  ,[ActorSurname]
  ,ROW_NUMBER() over(partition by filmname order by filmname) as rn
 FROM [LegOgSpass].[dbo].[actors]
 )X where X.rn > 1