如何(SELECT *)两个mysql表在node.js (express)
How to (SELECT * ) two mysql tables in node.js (express)
当用户单击一篇文章时,我希望显示文章以及保存在另一个表中的评论。但我的代码只加载帖子,而不是评论。这是我到目前为止的代码:
router.get('/reader', function(req, res){
//load the post - This works. The post/writing gets loaded
connection.query('SELECT * FROM posts WHERE ID = ?', req.query.id, function(err, result){
if(err){
throw err;
} else{
//now go through the second table for the comments. The problem is here I think
connection.query('SELECT * FROM comments WHERE post_ID = ?', req.query.id, function(err, comm){
if(err){
throw err;
} else{
//render the page while sending keys and values of the two queries' results arguments
res.render('reader', {print : result, comment: comm});
}
});
}
});
两个表的列数不同,因此UNION不起作用。我试过了。
您可以使用LEFT JOIN,因为您的帖子可能没有注释:
SELECT * FROM posts
LEFT JOIN comments
ON comments.post_ID = posts.ID
WHERE posts.ID = ?
相关文章:
- 如何使用Node/Express显示有关特定错误的自定义html页面
- renderReact/Rect Router+Node/Express.js的服务器端/同构渲染中未定义renderP
- 如何在不重新加载的情况下提交表单,并使用Node+Express将数据传递回同一页面
- 使用Node/Express随机查询Postgresql表并处理行数据
- 如何在node+express中重新运行请求处理程序
- 在Node Express中检测社交机器人
- Node Express Handlebars帮助程序未返回函数的结果
- Node/Express 无法正确更新数据模型
- 从 Node/Express/Angular/PhantomJS 导出带有图表的 PDF 报告
- Node+Express会话过期
- 将标志从Node/Express应用程序Python shell传递到Python脚本
- '角度'在Eclipse中Node Express项目的javascript文件中没有定义
- 如何使用Node Express提供静态文件
- Node-Express-Angular,为什么不加载JS
- Node/Express对MongoDB的AJAX调用示例
- Node/Express:根的路由标识符'/'路线
- 使用node/express在angular.js中不工作的路由
- 如何使用Jade正确地将Fusioncharts.js包含在Node Express项目中
- Node/Express - 如何等到 For 循环结束再使用 JSON 进行响应
- Angularjs route for node express js backend