MySQL SQL语法错误问题?

时间:2010-10-12 02:50:42

标签: sql mysql mysql-error-1064

我在下面收到以下错误,想知道如何修复它?

You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near '.user_id WHERE
users.active IS NULL AND users.deletion = 0) WHERE users.active' at line 4

这是我的MySQL代码(插入换行符,因此错误消息中的第4行对应于8)。

SELECT users.user_id, articles_comments.article_id, articles_comments.comment,
           articles_comments.comment_id
  FROM users_articles
 INNER JOIN articles_comments
            ON users_articles.id = articles_comments.article_id
 INNER JOIN (SELECT *
               FROM users INNER JOIN users_articles.user_id = users.user_id
               WHERE users.active IS NULL AND users.deletion = 0)
 WHERE users.active IS NULL
   AND users.deletion = 0
 ORDER BY articles_comments.date_created DESC
 LIMIT 50

2 个答案:

答案 0 :(得分:4)

SELECT users.user_id, articles_comments.article_id, articles_comments.comment, articles_comments.comment_id
FROM users_articles
INNER JOIN articles_comments ON users_articles.id = articles_comments.article_id
INNER JOIN (SELECT * FROM users INNER JOIN users_articles ON users_articles.user_id = users.user_id WHERE users.active IS NULL AND users.deletion = 0) x
WHERE x.active IS NULL
AND x.deletion = 0
ORDER BY articles_comments.date_created DESC
LIMIT 50

说明:

INNER JOIN users_articles.user_id = users.user_id

这是一个问题。您忘了指定已连接的表格和关键字ON

UPD :尝试此查询。它应该做同样的事情,但更具可读性

    SELECT *
      FROM users_articles a
INNER JOIN articles_comments c ON c.article_id = a.id
INNER JOIN users u ON u.user_id = a.user_id
                  AND u.active IS NULL
                  AND u.deletion = 0
  ORDER BY c.date_created DESC
     LIMIT 50

答案 1 :(得分:2)

错过了“ON”:

INNER JOIN (SELECT * FROM users INNER JOIN users_articles ON users_articles.user_id = users.user_id WHERE users.active IS NULL AND users.deletion = 0)

您可以通过this

更加了解