使用带有多个数据库表语句的`INNER JOIN`意味着什么?

时间:2012-06-26 15:02:18

标签: mysql sql ruby-on-rails database

我正在使用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 的数据库表。 是错误(即使按预期工作)

1 个答案:

答案 0 :(得分:4)

这是一个表别名。这意味着,它将表articles_comments_associations重命名为comment_associations_articles,以便在查询中进一步引用。任何字段或表都可以通过在表/字段引用之后给出另一个名称来别名。