简体   繁体   中英

Joining two tables and then selecting only info based on criteria in a third table

So I joined two tables but I only want to select info from that join based on criteria defined in another table. I only want info from the first join where the actors name is in the popactor table.

select  h.movie,
        h.released,
        a.rating,
        h.actor
  from  hollywood.stars h
    inner join starmovies a on h.movie =a.title
    inner join popactor p on p.actor = h.actor
  where h.movie = a.title and p.actor = h.actor;

First your where clause is useless as the 2 conditions are already in the ON clause of your joins so you could get rid of it. Then you have two ways to achieve what you are looking for : As suggested in comments :

select 
  h.movie,
  h.released,
  a.rating,
  h.actor
from 
  hollywood.stars h
  inner join starmovies a on h.movie = a.title
where h.actor IN (select actor FROM popactor) 

OR as you already did :

select  
  h.movie,
  h.released,
  a.rating,
  h.actor
from 
  hollywood.stars h
  inner join starmovies a on h.movie =a.title
  inner join popactor p on p.actor = h.actor

Of course, if h.actor nor p.actor is the actor name on which you want to filter, you need to replace these fields in both queries with the right ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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