【问题标题】:How to retrieve "common" records in the same database table through a single SQL query?如何通过单个 SQL 查询检索同一个数据库表中的“公共”记录?
【发布时间】:2012-07-14 20:48:19
【问题描述】:

我正在使用 Ruby on Rails(版本 3.2.2)和 mysql2(版本 0.3.11 - MySQL 版本 5.2.38)。在讨论“How to retrieve associated objects / records that a set of users have in "common" through a association table”* 时,我发现自己处于“特定”情况,我必须通过运行 single 来查找“关联对象/记录”** - 出于性能原因 - SQL 查询在关联数据库表上。特别是,我想这样做是为了检索前面提到的“关联对象/记录”,其中user_id(代表foreign key“指向”一个User 对象)article_id(表示foreign key“指向”Article 对象)列值相同(即,其中@ 987654330@ 和 user_id 是“常见的”)。

在我的情况下,我如何/应该检索“关联对象/记录”(也许,通过使用 Ruby on Rails 和/或 SQL/数据库“方式”/“上下文”中的某些工具)?


* 具体来说,在 @Andrew Marshall 发布他/她的答案之后。

** 注意:那些“关联对象/记录”与linked question 中描述的has_many :through Ruby on Rails ActiveRecord::Associations 相关。

【问题讨论】:

  • 什么意思,相关对象,例如所有购买过产品 X 的客户?
  • @Marc B - 我更新了问题,详细说明了它。
  • 可以提供模型关联吗?

标签: mysql sql ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

我认为这是一种提取所需内容的原始 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 方式来执行此操作...但这应该可以帮助您入门,您可以从这里重构它。

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2015-11-30
    相关资源
    最近更新 更多