次のテーブルを持つデータベースがあるとします。
以下を含む表movie
:
movie1
movie2
以下を含む表actors
:
actor1
actor2
actor3
actor2
映画movie1
との両方に登場するとしましょうmovie2
。actors
いくつかのスペースを節約するために、現在の俳優ごとに異なるレコードを入力してテーブルに独自の映画IDを与えることなく、これを使用するにはどうすればよいですか?
次のテーブルを持つデータベースがあるとします。
以下を含む表movie
:
movie1
movie2
以下を含む表actors
:
actor1
actor2
actor3
actor2
映画movie1
との両方に登場するとしましょうmovie2
。actors
いくつかのスペースを節約するために、現在の俳優ごとに異なるレコードを入力してテーブルに独自の映画IDを与えることなく、これを使用するにはどうすればよいですか?
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
「多対多」の関係がある場合は常に、前の回答に示されているようなリンク テーブルが必要です。