[英]What is the right way ? With javascript and mysql
I have a mysql table, named "cars", where I store : "brand", "model", "color". 我有一个名为“cars”的mysql表,我在其中存储:“品牌”,“模型”,“颜色”。 I would like to get the data like this :
我想得到这样的数据:
[
{
brand: "audi",
cars: [
{
"model": "coupe",
"color": "red",
},
{
"model": "a3",
"color": "blue",
},
]
},
{
brand: "renault",
cars: [
{
"model": "modus",
"color": "white",
},
{
"model": "clio",
"color": "green",
},
]
},
...
]
So, what I'm doing is a first mysql query where I group by brand, then I iterate the result to get all the cars for each brand : 所以,我正在做的是第一个mysql查询,我按品牌分组,然后我迭代结果以获得每个品牌的所有汽车:
const query = "SELECT brand FROM cars GROUP BY brand"
mysql.query(query, values, (err, result) => {
for (let i = 0; i < result.length; i++) {
const query2 = "SELECT model, color FROM cars WHERE brand = ?"
const values2 = [result[i].brand]
mysql.query(query2, values2, (err2, result2) => {
result[i].cars = result2
callback(result)
})
}
})
The code that I'm showing is working, but I don't think that doing an iteration for a mysql query is a good thing. 我正在展示的代码正在运行,但我不认为对mysql查询进行迭代是一件好事。
I've done a lot of research, and I don't really know what to do. 我做了很多研究,我真的不知道该怎么做。
Is it possible to get this result with one single mysql query ? 是否可以通过一个单独的mysql查询获得此结果? Or should I get all rows from the "cars" table and then do JS stuff to format the data ?
或者我应该从“cars”表中获取所有行,然后使用JS填充格式化数据? Or should I continue using this mysql query iteration ?
或者我应该继续使用这个mysql查询迭代?
Thank you 谢谢
You can do something like: 你可以这样做:
const carsByBrand=[],brands={};
function find(){
const query = "SELECT model, color FROM cars WHERE brand IN ('renault','bmw')"
mysql.query(query, values, (err, result) => {
/*assuming value to be like: [
{
brand:audi,
"model": "coupe",
"color": "red",
},
]
*/
values.forEach((car)=>{
if(brands[car.brand]){
carsByBrand[brands[car.brand]].cars.push(car)
}else{
let index=carsByBrand.push({
brand:car.brand,
cars:[car]
})
brands[car.brand]=index;
}
})
}
})
}
find()
Something like this should do the trick. 这样的事情应该可以解决问题。 Its client side transformation though:
它的客户端转型虽然:
let carArr = []
const query = "SELECT brand, model, color from cars"
mysql.query(query, values, (err, result) => {
for (let i = 0; i < result.length; i++) {
// Get the index of the brand in the carArr array
let brandIndex = carArr.map(c => c.brand).indexOf(result.brand)
// If the brand is not already in carArr, enter a new value for it
if(brandIndex == -1)
carArr.push({"brand": result.brand, "cars": [{"model": result.model, "color": result.color}]})
// If the brand already exists, add the car type to this specific barnd
else
carArr[brandIndex].cars.push({"model": result.model, "color": result.color})
}
})
We can achieve by single query itself by using GROUP_CONCAT
, CONCAT
mysql function. 我们可以通过使用
GROUP_CONCAT
, CONCAT
mysql函数来实现单个查询本身。 once you got result cars key will JSON string. 一旦你得到结果汽车钥匙将JSON字符串。 you can convert by using JSON.parse method
您可以使用JSON.parse方法进行转换
select brand,CONCAT("[",GROUP_CONCAT(CONCAT("{'model':'",model,"','color':'",color,"'}")),"]") as cars from cars GROUP BY brand;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.