如何查询多对多? [英] how to query many-to-many?
本文介绍了如何查询多对多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在观看Rails教程中的ruby时,我发现了以下表结构.
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?
我不要求在Rails代码上使用红宝石.我想要实际的mysql查询字符串.
I am not asking for ruby on rails code. I want the actual mysql query string.
谢谢!
推荐答案
要考虑的一件事是,您将要加载作者对象(由于RoR模型),因此使用ID就足够了:
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
这篇关于如何查询多对多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文