Table:articles
id address autor .........
1 name_test1 Paul ...........
2 name_test2 George ........
这个表包含一些博客文章。
Table:article-hearts
id cookie_userid article_id
1 123 1
2 345 1
3 123 2
当一个访客点击“心它!”动作记录在这里。
我想展示每一篇博文的心数。在这个语法中,我试图添加一个心数:
$articles = mysql_query("SELECT * FROM articles ORDER BY date_created, time_created DESC ") or die(mysql_error());
看起来是这样的:
$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‘加入其中
发布于 2015-02-12 12:31:03
有五个问题:
$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());
发布于 2015-02-12 12:22:18
你的语法错了。连接后必须是on
条件,而不是“`where”。此外,您还必须删除单引号在列名中的位置。
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
https://stackoverflow.com/questions/28477295
复制相似问题