mysql - 将 `INNER JOIN` 与多个数据库表语句一起使用时意味着什么/发生了什么?

标签 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 的数据库表。 这是错误吗(即使它按预期工作)

最佳答案

这是一个表别名。意思是,它正在将表 articles_comments_associations 重命名为 comment_associations_articles 以便在查询中进一步引用。只需在表/字段引用后提供另一个名称,即可为任何字段或表取别名。

关于mysql - 将 `INNER JOIN` 与多个数据库表语句一起使用时意味着什么/发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210241/

相关文章:

mysql - fullcalendar调度程序mysql资源json

mysql - 将 WHERE IN 参数视为 AND 而不是 OR

mysql - 无法理解 LIKE 行为

Mysql查询开始后回滚

ruby-on-rails - 了解 Ruby/Rails 中的 before_save

php - “DonutChart”不是有效的 Lavachart 对象

mysql - 改变 MySQL 列编码,级联到 FK

sql - 如何在选择查询中执行存储过程

ruby-on-rails - Heroku 无法转储匿名类

mysql - Spring MVC、MySQL 和 UTF-8