php - MySQL 查询无法正常工作

标签 php mysql sql select

错在哪里?

谢谢!

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

最佳答案

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 应该用反引号转义,因为它是 Reserved Keyword in MySQL

关于php - MySQL 查询无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879112/

相关文章:

php - 有一个输入 php 变量的 &lt;inputbox>

php - 是否有用于生成迁移 SQL (postgres) 文件的脚本?

php - 如何仅使用他们的 user_id 将用户登录到 Wordpress

php - 使用 php exec 函数执行 Node 命令时出错

mysql - 无法执行系统查询

mysql - 如何更新 bit(10) 数据类型列?

MySQL左连接使用单行

PHP 错误 : libphp5. 所以: undefined symbol :_estrndup

php - Laravel 路线未定义,但显然是

mysql - 如何对这样的 MySQL 表进行透视和计数?