I have two table
posts_table
post_id | user_id | status_message | date_time
comments_table
comments_id | user_id | post_id | comments_message | date_time
My code is this i want show every post and show all comments message in this post
router.get('/', (req, res, next) => {
connection.query('SELECT * FROM photos_status', (err, result) => {
if(err){
console.error(err);
}else{
if(result.length >0){
for(var i = 0; i < result . length ;i++){
var Temp = [];
var post_id =result[i]. post_id;
connection.query('SELECT * FROM comments WHERE post_id = ?',
[post_id], function (error, results) {
if (error) {
res.json({
status:false,
message:'there are some error with query'
})
}else{
res.status(200).json({
result ,
results
});
}
})
}
}
}
});
});
I want select data from database and show like this
[
{ "post_id":"1",
"user_id":"2",
"status_x":"demo ..."
" comments"[
{
"user_id":"1",
"post_id":"1",
"comments_message":"demo..",
},
{
"user_id":"2",
"post_id":"1",
"comments_message":"demo..",
}
]
}
]