【发布时间】:2019-07-04 02:14:44
【问题描述】:
我正在将我所在城市的主要大十字路口与我所在城市的碰撞数据汇总在一起。我试图完成的是确定在 20 米半径内的交叉路口及其周围发生的事故数量。
幸运的是,我的项目已经很遥远了,我的数据库中有两个表 intersections 和 collisions
我的intersections表:
--------------------------------------------
| id | city | Latitude | Longitude |
--------------------------------------------
| 1 | 1 | 34.44444 | 84.3434 |
--------------------------------------------
| 2 | 1 | 42.4666667 | 1.4666667 |
--------------------------------------------
| 3 | 1 | 32.534167 | 66.078056 |
--------------------------------------------
| 4 | 1 | 36.948889 | 66.328611 |
--------------------------------------------
| 5 | 1 | 35.088056 | 69.046389 |
--------------------------------------------
| 6 | 1 | 36.083056 | 69.0525 |
--------------------------------------------
| 7 | 1 | 31.015833 | 61.860278 |
--------------------------------------------
我的collisions表:
--------------------------------------------
| id | cause | Latitude | Longitude |
--------------------------------------------
| 1 | 1 | 44.44444 | 81.3434 |
--------------------------------------------
| 2 | 1 | 32.4666667 | 1.4666667 |
--------------------------------------------
| 3 | 1 | 42.534167 | 63.078056 |
--------------------------------------------
| 4 | 1 | 46.948889 | 62.328611 |
--------------------------------------------
| 5 | 1 | 45.088056 | 61.046389 |
--------------------------------------------
| 6 | 1 | 46.083056 | 63.0525 |
--------------------------------------------
| 7 | 1 | 41.015833 | 69.860278 |
--------------------------------------------
注意:为简单起见省略了一些字段
如您所见,两个表都拥有latitude 和longitude 字段。现在要确定 intersection 和 collision 坐标是否接近,我想到了使用带有 id 的 MySQL 查询intersection,然后查询 collisions 表以获取 intersection 20 米内的所有碰撞。
这个查询是什么(在 MySQL 或 Sequelize 中)?
这是我现在拥有的(它在 Sequelize 中使用 MySQL 数据库)
// api/collisions/{intersection_id}/count
exports.intersection_accident_count = (req, res) => {
intersection.findOne({where: {equipement: req.params.intersection_id}}).then(intersection => {
let intersectionLongitude = intersection.longitude;
let intersectionLatitude = intersection.latitude;
collision.count({where: {
longitude: {
$gt: intersectionLongitude
},
latitude: {
$gt: intersectionLatitude
},
}}).then(count => {
res.status(200).json({'number_of_accidents': count});
});
});
};
【问题讨论】:
标签: javascript mysql node.js express sequelize.js