我正在使用 Ruby on Rails 3.2.2 和 MySQL。我有一个生成以下 SLQ 查询的方法(has_many :through
ActiveRecord::Associations
):
SELECT DISTINCT `articles`.*
FROM `articles`
INNER JOIN `articles_comments_associations` `comment_associations_articles`
ON `comment_associations_articles`.`article_id` = `articles`.`id`
INNER JOIN `articles_comments_associations`
ON `articles`.`id` = `articles_comments_associations`.`article_id`
WHERE `articles_comments_associations`.`comment_id` = 223
AND (articles_comments_associations.user_id IN (2))
我想了解一下 INNER JOIN 'articles_comments_associations' 'comment_associations_articles'
是什么意思(注意:INNER JOIN< 有多个数据库表语句
) 以及 SQL 查询如何工作,因为我没有名为 comment_associations_articles
的数据库表。 这是错误吗(即使它按预期工作)?
最佳答案
这是一个表别名。意思是,它正在将表 articles_comments_associations
重命名为 comment_associations_articles
以便在查询中进一步引用。只需在表/字段引用后提供另一个名称,即可为任何字段或表取别名。
关于mysql - 将 `INNER JOIN` 与多个数据库表语句一起使用时意味着什么/发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210241/