我将制作一个相当简单的RIGHT JOIN查询,但我无法正确格式化输出。
以下是查询语句:
connection.query({sql : "SELECT users.*, rides.* FROM users RIGHT JOIN rides ON users.id = rides.id_user WHERE users.id = ?", nestTables: '_', values : [id] }, function(err, rows){
console.log(rows);
});
这是我的输出结果:
[ { users_id: 52,
users_firstname: 'greg', //End first table data
rides_latitude: '50.847454', //Second table data: row 1
rides_longitude: '4.358356',
},
{ users_id: 52,
users_firstname: 'greg', //Exactly the same first table data
rides_latitude: '50.9', //Second table data: row 2
rides_longitude: '4.4',
} ]
我希望您能够提供如下输出:
[ { users_id: 52,
users_firstname: 'greg',
rides : [
{
rides_latitude: '50.847454',
rides_longitude: '4.358356'
},
{
rides_latitude: '50.9',
rides_longitude: '4.4'
}
]
}]
我尝试了nestTables
,如您所见,
key:val
格式表示的数据。现在,您需要根据自己的需求自定义结果,或者检查是否有中间件或辅助模块可用于此目的。 - Abhik Chakraborty