-2

次のテーブルを持つデータベースがあるとします。

以下を含む表movie:

movie1
movie2

以下を含む表actors:

actor1
actor2
actor3

actor2映画movie1との両方に登場するとしましょうmovie2actorsいくつかのスペースを節約するために、現在の俳優ごとに異なるレコードを入力してテーブルに独自の映画IDを与えることなく、これを使用するにはどうすればよいですか?

4

2 に答える 2

2

I think what you are looking for is a junction-table (link-table) (http://en.wikipedia.org/wiki/Junction_table):

(Just for the sake of it I set plural on both table-names)

tab movies 
      = movie1
      = movie2

tab actors
      = actor1
      = actor2
      = actor3

tab movies_actors
      = actors (id of tab_actors)
      = movies (id of tab_movies)

Then you could join tab_movies_actors with tab_movies and tab_actors to get which actors belongs to which movies:

SELECT * FROM tb_movies tm
JOIN tb_movies_actors tma ON (tm.id = tma.movies_id)
JOIN tb_actors ta ON  (tma.actors_id = ta.id)

or a specific movie:

SELECT * FROM tb_movies tm
JOIN tb_movies_actors tma ON (tm.id = tma.movies_id)
JOIN tb_actors ta ON  (tma.actors_id = ta.id)
WHERE tm.id = 10
于 2013-08-25T12:35:12.533 に答える
0

「多対多」の関係がある場合は常に、前の回答に示されているようなリンク テーブルが必要です。

于 2013-08-25T12:56:32.370 に答える