views:

33

answers:

2

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
+4  A: 
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
zerkms
Now I get Every derived table must have its own alias?
blah
(SELECT * FROM users INNER JOIN users_articles.user_id = users.user_id WHERE users.active IS NULL AND users.deletion = 0) AS derived
tandu
i've added `x` alias. by the way - the more correct way will be to describe what you want to get, because this query is terrible :-S
zerkms
@zerkms I will ask that as my next question then.
blah
Now I got the following error Duplicate column name 'user_id' :(
blah
@blah: i've rewritten it. try the query from **UPD**
zerkms
@zerkms The UPD query wont check for the fields `AND u.active IS NULL AND u.deletion = 0`
blah
@blah: It is checked in the `ON` clause
zerkms
@zerkms no it doesn't I see results that should not appear:(
blah
@blah: Give the sample output that my query returns. And explain what is wrong there.
zerkms
@zerkms All users and there comments that are marked for `u.deletion = 1` 1 equals deletion should NOT appear as well as users and their comments that have not activated their account `u.active IS NOT NULL` IS NOT NULL equals not activated.
blah
@blah: please - provide query result, i cannot get what you're trying to describe. There are 2 conditions about active and deletion.
zerkms
how can I provide the query result then?
blah
@blah: edit your question and add few lines with the only key columns, that do not satisfy your expectations.
zerkms
you lost me an example would help me understand what you are talking about?
blah
@blah: scroll to the top and add edit your question ;-) add result with lines that do not fit the conditions. ;-)
zerkms
but all you will be able to see is the comments
blah
@blah: so? do you want to get help or not? :-) my query supposed to work the same as yours. I do not have another information about how it should work, and your attempts to describe failed to me :-S
zerkms
I' try to work out the problem my self for right now and if i get stuck again I'll ask a new question thanks for the help though
blah
Ok ;-) good luck ;-)
zerkms
+2  A: 

missing "ON" at :

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)

you can more understand with this

klox