当用户单击帖子时,我希望显示文章以及保存在其他表格中的评论。但是我只加载了代码,而没有评论。这是我到目前为止的代码:
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 = ?
关于javascript - 如何(SELECT *)在node.js中的两个mysql表(express),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33375649/