我有这个原始的SQL查询。
SELECT
restaurants.name AS restaurant,
ROUND((6371 * ACOS(COS(RADIANS(6.9271)) * COS(RADIANS(restaurants.latitude)) *
COS(RADIANS(restaurants.longitude) - RADIANS(79.8612)) + SIN(RADIANS(6.9271)) *
SIN(RADIANS(restaurants.latitude)))),
2) AS distance,
dishes.price
FROM
restaurants
INNER JOIN
dishes ON restaurants.id = dishes.restaurantId
WHERE
restaurants.status = TRUE
ORDER BY distance
LIMIT 200;
我确实希望将此查询转换为Sequelize查询。 我到目前为止所做的是
const data = await db.restaurant.findAll({
attributes: ['name'],
include: [{ model: db.dish, attributes: ['price'] }],
where: {
status: true,
},
limit: 200,
});
如何将此距离内容添加到查询中?
答案 0 :(得分:1)
您可以使用sequelize.literal()
来包含正弦距离-
attributes: [
'name',
[sequelize.literal(`
ROUND((6371 * ACOS(COS(RADIANS(6.9271)) * COS(RADIANS(restaurants.latitude)) *
COS(RADIANS(restaurants.longitude) - RADIANS(79.8612)) + SIN(RADIANS(6.9271)) *
SIN(RADIANS(restaurants.latitude)))), 2)
`, 'distance'],
],