在sql中连接表并根据查询获取数据

时间:2022-03-07 16:28:43

I have three tables( movie,actor,casting). I want to know the actors name for the id obtained from this query.

我有三张桌子(电影,演员,演员)。我想知道从这个查询中获取的id的actor名称。

select id from movie where title ='Casablanca';

My tables:

我的桌子:

Movie   | Actor | casting
_______  ________   _______

Movieid   Actorid   Movieid
title     name      Actorid
yr        ord
director        
budget      
gross   

2 个解决方案

#1


1  

This should do it:

这应该这样做:

SELECT a.name
FROM movie m
INNER JOIN casting c
  ON m.id = c.movieid
INNER JOIN actor a
  ON c.actorid = a.id
WHERE m.title = 'Casablanca';

#2


1  

Try this:

尝试这个:

SELECT a.id, a.name 
FROM actor a 
INNER JOIN casting c 
        ON a.id = c.actorid
INNER JOIN movie m 
        ON c.movieid = m.id
WHERE m.title ='Casablanca';

#1


1  

This should do it:

这应该这样做:

SELECT a.name
FROM movie m
INNER JOIN casting c
  ON m.id = c.movieid
INNER JOIN actor a
  ON c.actorid = a.id
WHERE m.title = 'Casablanca';

#2


1  

Try this:

尝试这个:

SELECT a.id, a.name 
FROM actor a 
INNER JOIN casting c 
        ON a.id = c.actorid
INNER JOIN movie m 
        ON c.movieid = m.id
WHERE m.title ='Casablanca';