SQL Query - Select Query

自闭症网瘾萝莉.ら 提交于 2019-12-12 01:47:37

问题


Here are my database relations:

shows(showID, title, premiere_year, network, creator, category)

episode(showID, episodeID, airdate, title)

  • showID is a foreign key to shows

actor(actID, fname, lname)

main_cast(showID, actID, role)

  • showID is a foreign key to shows actID is a foreign key to actor

recurring_cast(showID, episodeID, actID, role)

  • showID is a foreign key to shows episodeID is a foreign key to episode actID is a foreign key to actor

customer(custID, fname, lname, email, creditcard,membersince,renewaldate, password, username)

cust_queue(custID, showID, datequeued)

  • custID is a foreign key to customer showID is a foreign key to shows

watched(custID, showID, episodeID, datewatched)

  • custID is a foreign key to customer
  • showID is a foreign key to shows
  • (showID, episodeID) is a foreign key to episode
  • (custID, showID) is a foreign key to cust_queue

All the 'IDs' are primary keys


I have queries I was given and for some, I don't know how to go about it. Such as:

Find all actors who are in the main cast of at least one show and in the recurring cast of at least one show. Display the actor's first name, last name, the title of the show in which the actor is in the main cast, the title of the show in which the actors is in the recurring cast, and the role the actor plays in each show.

I'm trying:

{
    SELECT Actor.fname, Actor.lname, Shows.Title
    FROM Actor, Shows, Main_Cast, Recurring_Cast
    WHERE Actor.actID = Main_Cast.actID AND Actor.actID = Recurring_Cast.actID;
}

But I don't think that's right. Any ideas??


回答1:


Try the following:

SELECT Actor.fname, Actor.lname, Shows.Title
FROM Actor, Shows  AS sh
WHERE Actor.actID 
IN 
      (SELECT actID FROM Main_Cast
      WHERE sh.showID==Main_Cast.showID)
UNION
      (SELECT actID FROM Recurring_Cast
      WHERE sh.showID==Recurring_Cast.showID)

This will show you the actors who are in the main cast and recurring cast of the same show. You can edit it a little to get what you want.



来源:https://stackoverflow.com/questions/29151114/sql-query-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!