0

I'd like to select from multiple identically constructed tables returning values of three columns for each row where a value of one of the columns is found by another query. For example, an inefficient way to do this might be:

Select Film_Title, Main_Actor, Run_Time From Film_Table_1
  Where Film_Title in
       (Select Film_Title
          From Awesome_Films_Table
          Where Film_Year equals "1984")
UNION ALL
Select Film_Title, Main_Actor, Run_Time From Film_Table_2
  Where Film_Title in
       (Select Film_Title
          From Awesome_Films_Table
          Where Film_Year equals "1984")
Select Film_Title, Main_Actor, Run_Time From Film_Table_3
  Where Film_Title in
       (Select Film_Title
          From Awesome_Films_Table
          Where Film_Year equals "1984")
Select Film_Title, Main_Actor, Run_Time From Film_Table_4
  Where Film_Title in
       (Select Film_Title
          From Awesome_Films_Table
          Where Film_Year equals "1984");

In reality, I have many 'Film_Table_#' to select from. Can anyone advise how to achieve this more efficiently? I presume replacing:

Select Film_Title
            From Awesome_Films_Table
            Where Film_Year equals "1984"

That I currently have on each Where specifier with a single variable defined at the start might help, but I'm not sure how that might be achieved.

4

1 回答 1

0

在 Oracle 中是这样的:

  1. equals 应该被设为 =
  2. " 应该被做成 '
  3. 最糟糕的设计是每部电影只有一张桌子。理想情况下,它应该是集中表中每部电影的一行(如果需要,请使用 NF)

    WITH AFT
      AS (SELECT
               FILM_TITLE TITLE
          FROM
               AWESOME_FILMS_TABLE
          WHERE
               FILM_YEAR = '1984')
    SELECT
        FILM_TITLE,
        MAIN_ACTOR,
        RUN_TIME
    FROM
        FILM_TABLE_1,
        AFT
    WHERE
        FILM_TITLE = TITLE
    UNION ALL
    SELECT
        FILM_TITLE,
        MAIN_ACTOR,
        RUN_TIME
    FROM
        FILM_TABLE_2,
        AFT
    WHERE
        FILM_TITLE = TITLE
    UNION ALL
    SELECT
        FILM_TITLE,
        MAIN_ACTOR,
        RUN_TIME
    FROM
        FILM_TABLE_3,
        AFT
    WHERE
        FILM_TITLE = TITLE
    UNION ALL
    SELECT
        FILM_TITLE,
        MAIN_ACTOR,
        RUN_TIME
    FROM
        FILM_TABLE_4,
        AFT
    WHERE
        FILM_TITLE = TITLE;</li>
    

于 2013-10-15T15:10:58.560 回答