1

Where is the mistake?

Thank you!

SELECT Table.id,
       Table.name,
   Table.kommentar,
   Table.pictureurl

   WHERE Table.news_id = 5, //without this line the query works!

   COUNT( Table1.comment_id ) AS numComments
   FROM DATABASE.news_comments          AS Table
   LEFT JOIN DATABASE.news_comments_comments AS Table1
   ON Table1.comment_id = Table.id

   GROUP BY Table.id
   ORDER BY Table.id DESC LIMIT 0,50

2 Answers 2

5

WHERE clause should be after the FROM clause

SELECT `Table`.id,
       `Table`.name,
       `Table`.kommentar,
       `Table`.pictureurl,
       COUNT( Table1.comment_id ) AS numComments
FROM   DATABASE.news_comments          AS `Table`
          LEFT JOIN DATABASE.news_comments_comments AS Table1
             ON Table1.comment_id = `Table`.id
WHERE `Table`.news_id = 5    // <=== HERE
GROUP BY `Table`.id
ORDER BY `Table`.id DESC LIMIT 0,50

One more thing, your alias which is Table should be escape with backtick since it a Reserved Keyword in MySQL

Sign up to request clarification or add additional context in comments.

5 Comments

table needs to be escaped with backticks in as table
@Darwin: It is already fixed. The part DATABASE.news_comments AS Table needed backticks around table since table is a reserved word.
@Darwin Originally the table was not escaped. Thanks to juergen. :)
OK, so TABLE was just a placeholder for me. In my case it is something like user_blablabla, which mean that it does not need any back ticks, right? Thank you all!
Yes, backticks is used to escape reserved words. :)
0
SELECT Table.id,
       Table.name,
   Table.kommentar,
   Table.pictureurl,   
   COUNT( Table1.comment_id ) AS numComments
   FROM DATABASE.news_comments  AS Table
   LEFT JOIN DATABASE.news_comments_comments AS Table1
   ON Table1.comment_id = Table.id

WHERE Table.news_id = 5   //===> where should be here

   GROUP BY Table.id
   ORDER BY Table.id DESC LIMIT 0,50

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.