Search code examples
sqlmysqlmysql-error-1064

MySQL SQL syntax error problem?


I get the following error below and was wondering how can I fix it?

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

And here is my MySQL code (line breaks inserted so line 4 in error message corresponds to 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

Solution

  • 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
    

    Explanation:

    INNER JOIN users_articles.user_id = users.user_id
    

    This is an issue. You forgot to specify joined table and keyword ON

    UPD: try this query. It should do the same stuff but much more readable

        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