您好我正在尝试使用Sequelize执行基于位置的查询,而我正在做一场噩梦!
我正在尝试生成SQL:
=SUM(INDEX(2:5,0,MATCH(J1,1:1,0)))
其中[user-latitude]和[user-longitude]是变量。到目前为止,我有这个:
SELECT *, (3959 * acos(cos(radians([user-latitude])) * cos(radians(latitude)) * cos(radians(longitude) - radians([user-longitude])) + sin(radians([user-latitude])) * sin(radians(latitude)))) AS distance FROM myModel HAVING distance <= 25 ORDER BY distance ASC LIMIT 0 , 10;
生成:
myModel.findAll({
attributes: [
'*',
[`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`, 'distance'],
],
where: {
distance: {
$lte: 25,
},
},
order: [
['distance', 'ASC'],
],
limit: 10,
offset: 0,
});
不起作用,因为`myModel` .distance`不是字段。有没有办法在不使用原始查询的情况下完成这项工作?
答案 0 :(得分:2)
很遗憾,您无法在WHERE
或HAVING
语句中使用别名字段,只能在ORDER BY
中使用。您必须在WHERE
子句中重复您的语句,而不是使用别名(就像SQL Use alias in Where statement中所述)。
更重要的是,您获得的错误是因为当您在distance
和where
属性中使用字段order
时,Sequelize会自动将其视为myModel
的字段而是你的alias
,所以你需要按字面意思写它,这样它就不会被视为你选择的一列表。
myModel.findAll({
attributes: {
include: [[`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`, 'distance']]
},
where: sequelize.where(
sequelize.literal(`(3959 * acos(cos(radians(${user-latitude})) * cos(radians(latitude)) * cos(radians(longitude) - radians(${user-longitude})) + sin(radians(${user-latitude})) * sin(radians(latitude))))`),
'<=',
25
),
order: 'distance ASC',
limit: 10,
offset: 0
});
在这种情况下, sequelize
是你的Sequelize实例。