【发布时间】:2019-09-30 12:47:54
【问题描述】:
我有这个带有 INNER JOIN 的 mySQL 查询,我想将它与 Handlebars 一起使用,虽然它是单次返回,但它仍然返回多个结果,因为图像表包含更多结果。
如何返回单个结果,然后将图像放在单独的 json 字符串中?
这里是查询:
app.get("/car/:brand-:model-:id", function (req, res) {
let userName;
if (req.user != undefined) {
userName = req.user.displayName;
}
const carSql = `SELECT * FROM cars INNER JOIN carimage ON cars.id = carimage.carId WHERE carimage.carId = ${req.params.id}`;
const carQuery = db.query(carSql, (err, results) => {
if (err) throw err;
const data = {
car: results,
username: userName
}
//res.render("pages/car", data);
res.send(data);
});
});
然后返回这个 JSON:
{
car: [
{
id: 274,
userId: 10157597945502792,
brand: "Ford",
carId: 246,
image: "carimages-1569568518629.jpg"
},
{
id: 275,
userId: 10157597945502792,
brand: "Ford",
carId: 246,
image: "carimages-1569568518649.jpg"
}
]
}
我想要的是将结果合并为一个结果,然后将图像作为嵌套 JSON。
这可能吗?
【问题讨论】:
-
添加
GROUP BY cars.carId
标签: mysql node.js json handlebars.js