Msg 4104使用INNER JOINS - SQL SERVER 2012

时间:2013-04-21 00:02:42

标签: sql tsql sql-server-2012-express

我有这些表格:

我正在尝试使用连接表article_id, article_name, article_title, article_descriptionarticle_date articlesusername获取usersuser_articles

我试过这段代码:

SELECT E.article_id, E.article_title, E.article_description, E.article_date, u.username
FROM articles AS E, users as u
INNER JOIN user_articles as A ON A.article_id = E.article_id
INNER JOIN users as u2 ON u2.user_id = A.user_id
WHERE E.article_status = 1

我收到此错误消息:

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "E.article_id" could not be bound.

enter image description here

我正在使用SQL Server 2012 Express。

3 个答案:

答案 0 :(得分:2)

您不能从一个表中选择并在另一个表上应用联接。试试这个问题:

SELECT E.article_id, E.article_title, E.article_description, E.article_date, u.username
FROM articles AS E
    INNER JOIN user_articles as A ON A.article_id = E.article_id
    INNER JOIN users as u ON u.user_id = A.user_id
WHERE E.article_status = 1

答案 1 :(得分:2)

问题是你正在混合JOIN类型,你有逗号分隔的隐式JOIN,然后是显式的JOIN。你的代码应该是:

SELECT E.article_id, 
  E.article_title, 
  E.article_description, 
  E.article_date, 
  u.username
FROM articles AS E
INNER JOIN user_articles as A 
  ON A.article_id = E.article_id
INNER JOIN users as u
  ON A.user_id = u.user_id
WHERE E.article_status = 1;

答案 2 :(得分:0)

我在想你为什么甚至需要这样一个表:users_articles? 将名为(article_author)的列添加到使用用户的id填充的articles表中会不会更有效率?那么你就可以加入那个价值了。只是说