【发布时间】:2021-08-20 05:17:21
【问题描述】:
我想在 api 端点接收的数据是:
[
{"Products":5,"Computer":2,"Handphone":3},
{"Fruits":4,"Orange":2,"Apple":2},
{"Consumables":6,"Tissue":2,"Wet Wipes":3, "Handkerchief":1}
]
所以产品类别包含电脑、手机。水果类包括橙子和苹果。消耗品类包括纸巾、湿纸巾和手帕
我在 mySQL 中有以下数据:
| ID | Items | Category |
|---|---|---|
| 1 | Computer | Products |
| 2 | Handphone | Products |
| 3 | Computer | Products |
| 4 | Handphone | Products |
| 5 | Handphone | Products |
| 6 | Orange | Fruits |
| 7 | Orange | Fruits |
| 8 | Apple | Fruits |
| 9 | Apple | Fruits |
| 10 | Tissue | Consumables |
| 11 | Tissue | Consumables |
| 12 | Wet Wipes | Consumables |
| 13 | Wet Wipes | Consumables |
| 14 | Handkerchief | Consumables |
| 15 | Wet Wipes | Consumables |
这是我用来在反应中进行数据库查询的代码:
app.get("/countintents", (req, res) => {
db.query(
"SELECT SUM(CASE WHEN intent IN ('Products', 'Land Transport') THEN 1 END) AS products, SUM(CASE WHEN intent IN ('Air Freight', 'Industry Solutions', 'Aerospace and Defense') THEN 1 END) AS industry_solutions, SUM(CASE WHEN intent in ('Profile', 'Contact Us') THEN 1 END) AS about_us FROM data_analytics",
(err, result) => {
if (err) {
console.log(err);
} else {
res.send(result);
}
}
);
});
我会问我的数据库查询应该是什么?
【问题讨论】:
标签: javascript mysql sql node.js reactjs