I found the following table structures while I was watching ruby on rails tutorial.
table actors id int 11 primary key auto_increment name varchar 30 table movies id int 11 primary key auto_increment name varchar 30 table actors_movies actor_id int 11 movie_id int 11
How do I make a query to select movies that an actor is involved in?
I am not asking for ruby on rails code. I want the actual mysql query string.
Thank you!
When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.
Maybe something like this:
select m.name from movies m inner join actors_movies am on m.id = am.movie_id inner join actors a on am.actor_id = a.id where a.name = 'Christopher Walken'
one thing to consider is that you are going to load the author object (because of RoR models), so with the ID would be enough:
select movies.id, movies.name from movies inner join actors_movies on actors_movies.movie_id=movies.id where actors_movies.actor_id=$actor_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With