gpt4 book ai didi

sequelize.js - Sequelize 如何将复杂的原始查询转换为 Sequelize 查询

转载 作者:行者123 更新时间:2023-12-03 22:26:46 32 4
gpt4 key购买 nike

我有这个原始 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,
});

我如何将这个 距离 添加到我的查询中?

最佳答案

您可以使用 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'],
],

关于sequelize.js - Sequelize 如何将复杂的原始查询转换为 Sequelize 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765248/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com