首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MYSQL连接和计数语法错误

MYSQL连接和计数语法错误
EN

Stack Overflow用户
提问于 2015-02-12 12:18:19
回答 2查看 47关注 0票数 0
代码语言:javascript
运行
复制
Table:articles
id  address     autor   .........
1   name_test1  Paul    ...........
2   name_test2  George  ........

这个表包含一些博客文章。

代码语言:javascript
运行
复制
Table:article-hearts
id  cookie_userid   article_id
1   123             1
2   345             1
3   123             2

当一个访客点击“心它!”动作记录在这里。

我想展示每一篇博文的心数。在这个语法中,我试图添加一个心数:

代码语言:javascript
运行
复制
$articles = mysql_query("SELECT * FROM articles ORDER BY date_created, time_created DESC ") or die(mysql_error());

看起来是这样的:

代码语言:javascript
运行
复制
 $articles = mysql_query("SELECT a.id, a.address, a.autor, a.date_created, a.time_created, a.title, a.content, a.category, a.reads, ah.cookie_userid, ah.article_id, ah.id, 
COUNT(*) as 'hcount'
FROM
articles AS a
    join 'article_hearts' AS ah where 'ah.article_id' = 'a.id'
    join 'article_hearts' AS ah2 where ah2.article_id=ah.article_id
ORDER BY a.date_created, a.time_created DESC
")or die(mysql_error());

我得到的错误是,第5行出了问题:将'article_hearts‘作为'ah.article_id’= 'a.id‘加入其中

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-12 12:31:03

有五个问题:

  • 表中的引号
  • 两次在桌子上打牌
  • 联接语法不良
  • 失踪组
  • 在select中不需要某些列 $articles = mysql_query( "SELECT a.id, a.address, a.autor, a.date_created, a.time_created, a.title, a.content, a.category, a.reads, COUNT(*) as 'hcount' FROM articles AS a JOIN article_hearts AS ah ON ah.article_id = a.id GROUP BY a.id, a.address, a.autor, a.date_created, a.time_created, a.title, a.content, a.category, a.reads ORDER BY a.date_created, a.time_created DESC") or die(mysql_error());
票数 0
EN

Stack Overflow用户

发布于 2015-02-12 12:22:18

你的语法错了。连接后必须是on条件,而不是“`where”。此外,您还必须删除单引号在列名中的位置。

代码语言:javascript
运行
复制
SELECT a.id, a.address, a.autor, a.date_created, a.time_created, a.title, a.content, a.category, a.reads, ah.cookie_userid, ah.article_id, ah.id, 
COUNT(*) as 'hcount'
FROM
articles AS a
    join 'article_hearts' AS ah on ah.article_id= a.id
    join 'article_hearts' AS ah2 on ah2.article_id=ah.article_id
ORDER BY a.date_created, a.time_created DESC
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28477295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档