You can use JOINS
for this.
In a brief intro, lets have another table, relations
. In that, the movies and the actors are related. The PRIMARY KEY
will be the combination of the both ID
s.
Actors Table
+----+----------------+
| ID | NAME |
+----+----------------+
| 1 | Brad Pitt |
| 2 | Edward Norton |
| 3 | Jack Nicholson |
+----+----------------+
Movies Table
+----+--------------------+
| ID | NAME |
+----+--------------------+
| 1 | Fight Club |
| 2 | Ocean''s Thirteen. |
+----+--------------------+
Now we can have a relationship table with these two IDs.
+-------+-------+
| MOVIE | ACTOR |
+-------+-------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
+-------+-------+
This way, Movie 1 will have both actors 1 and 2.
SQL Queries
CREATE TABLE Actors (`id` int, `name` varchar(255));
INSERT INTO Actors (`id`, `name`) VALUES
(1, 'Brad Pitt'),
(2, 'Edward Norton'),
(3, 'Jack Nicholson');
CREATE TABLE Movies (`id` int, `name` varchar(255));
INSERT INTO Movies (`id`, `name`) VALUES
(1, 'Fight Club'),
(2, 'Ocean''''s Thirteen.');
CREATE TABLE stars (`movie` int, `actor` int);
INSERT INTO RelationShip (`movie`, `actor`) VALUES
(1, 1),
(1, 2),
(2, 1);