Mysql 如何通过单个SQL查询检索同一数据库表中的“公共”记录?
我使用的是RubyonRails版本3.2.2和mysql2版本0.3.11-MySQL版本5.2.38。在讨论*时,我发现自己处于一种特定的情况,在这种情况下,我必须通过对关联数据库表运行单个SQL查询(出于性能原因)来查找关联对象/记录**。特别是,为了仅检索前面提到的关联对象/记录,其中表示指向用户对象的外键的user_id和表示指向项目对象的外键的article_id列值相同,也就是说,article_id和user_id是公共的 在我的例子中,我应该如何使用RubyonRails和/或SQL/数据库方式/上下文中的一些工具来检索相关的对象/记录 *具体来说,在@Andrew Marshall发布了他/她的答案之后Mysql 如何通过单个SQL查询检索同一数据库表中的“公共”记录?,mysql,sql,ruby-on-rails,ruby,ruby-on-rails-3,Mysql,Sql,Ruby On Rails,Ruby,Ruby On Rails 3,我使用的是RubyonRails版本3.2.2和mysql2版本0.3.11-MySQL版本5.2.38。在讨论*时,我发现自己处于一种特定的情况,在这种情况下,我必须通过对关联数据库表运行单个SQL查询(出于性能原因)来查找关联对象/记录**。特别是,为了仅检索前面提到的关联对象/记录,其中表示指向用户对象的外键的user_id和表示指向项目对象的外键的article_id列值相同,也就是说,article_id和user_id是公共的 在我的例子中,我应该如何使用RubyonRails和/或
**注意:这些关联的对象/记录与中描述的通过Ruby on Rails的has\u many:相关。这是一种原始sql方法,可以提取您想要的内容,我认为:
select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;
您可以在Rails中替换用户ID的位置:
ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id = ?", @user1.id, @user2.id])
或对于多个ID::
ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id IN (?)", @user1.id, [@user2.id,@user3.id]])
因此,根据您其他问题的样本数据:
mysql> select * from articles_users;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 3 | 1 |
| 7 | 3 | 3 |
| 8 | 4 | 4 |
+----+---------+------------+
8 rows in set (0.00 sec)
它将返回如下值:
mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
| 4 | 2 | 1 |
| 5 | 2 | 2 |
+----+---------+------------+
2 rows in set (0.00 sec)
mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 3;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
| 6 | 3 | 1 |
| 7 | 3 | 3 |
+----+---------+------------+
2 rows in set (0.00 sec)
或对于多个用户ID:
mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id in (2,3);
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 3 | 1 |
| 7 | 3 | 3 |
+----+---------+------------+
4 rows in set (0.00 sec)
您要求使用sql方式,但几乎可以肯定有一种railsy方式可以做到这一点。。。但是这应该让你开始,你可以从这里重构它。你说的相关对象是什么意思,例如购买CD产品X的所有客户?@Marc B-我更新了问题,详细说明了它。你能提供模型关联吗?