1

错误在哪里?

谢谢!

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
4

2 回答 2

5

WHERE子句应该在FROM子句之后

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

还有一件事,你的别名Table应该用反引号转义,因为它是MySQL 中的保留关键字

于 2012-08-09T08:05:04.420 回答
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
于 2012-08-09T08:05:14.963 回答