如何从用户当前所在的位置(“位置”)查找经纬度附近的商店

时间:2019-07-16 16:05:01

标签: express sequelize.js

我正在尝试通过提供3个参数(例如user_id,纬度和经度)从用户当前位置查找附近的商店。

“商店表”包含有关商店的所有详细信息,例如名称,地址,纬度,经度等。

所以我想获取附近的所有商店。

我已经尝试过了,但是它给了我语法错误...

这些是参数:

make: *** No rule to make target `/Users/hareentej22/Downloads/wxwidgets2.8-2.8.12.1/src/png/png.c', needed by `wxpng_png.o'.  Stop.

它给了我语法错误。...

latitude = 49.4084659,
longitude = 8.834348800000043,
user_id = 1

const query = `
    (
        3959 * acos(
            cos( radians(49.4084659) ) * cos( radians( store_latitude ) ) * cos( radians( store_logitude ) - radians(8.834348800000043) )
            +
            sin( radians(49.4084659) ) * sin( radians( store_latitude ) )
        )
    ) AS distance FROM store WHERE user_id IN (
        SELECT \`Follows\`.receiver_id FROM \`follows\` AS \`Follows\` WHERE \`Follows\`.user_id=${user_id} and \`Follows\`.status="accept"
    ) HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
`;

store.findAll({
    where: {
        [Op.or]: [{
            user_id: {
                [Op.in]: [
                    sequelize.literal(query)
                ]
            }
        }, {
            user_id: user_id
        }]
    }
});

此MYSQL查询工作正常,并为我提供了所需的结果。

Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 
        'AS distance FROM stores WHERE user_id IN (SELECT `Follows`.receiver_id FROM `foll' at line 1

有人可以将其转换为Sequelize查询吗?...

3 个答案:

答案 0 :(得分:0)

您的AS前没有空格。

 ) ) )AS distance

就像@kshetline建议的那样,我认为如果您稍微简化代码格式,可能会使这些事情更容易发现。

答案 1 :(得分:0)

要学习调试问题,请先正确格式化代码。看看:

const query = `
    (
        3959 * acos(
            cos( radians(19.1250432) ) * cos( radians( store_latitude ) ) * cos( radians( store_logitude ) - radians(72.93173759999999) )
            +
            sin( radians(19.1250432) ) * sin( radians( store_latitude ) )
        )
    ) AS distance FROM store WHERE user_id IN (
        SELECT \`Follows\`.receiver_id FROM \`follows\` AS \`Follows\` WHERE \`Follows\`.user_id=${user_id} and \`Follows\`.status="accept"
    ) HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
`;

store.findAll({
    where: {
        [Op.or]: [{
            user_id: {
                [Op.in]: [
                    sequelize.literal(query)
                ]
            }
        }, {
            user_id: user_id
        }]
    }
});

好多了!

答案 2 :(得分:0)

MYSQL查询

SELECT store_id,user_id,store_name, 
    (   
        3959 * acos( cos( radians(49.4084659) ) * cos( radians( store_latitude ) ) * cos( radians( event_logitude ) - radians(8.834348800000043) ) 
        + 
       sin(radians(49.4084659) ) * sin( radians( store_latitude ) ) ) 
    ) AS distance FROM posts WHERE user_id IN (
       SELECT receiver_id FROM follows WHERE user_id = 1 AND status ="accept") OR user_id = 1 
       HAVING distance < 25 ORDER BY distance LIMIT 0 , 10

以上查询可以按顺序转换为

const lat = 49.4084659;
const lon = 8.834348800000043;
const user_id = 1;
const km = 6371;

const query = '
        ( '+km+' * acos( cos( radians('+lat+') ) * cos( radians( store_latitude ) ) * cos( radians( store_logitude ) - radians('+lon+') ) 
            + 
            sin( radians('+lat+') ) * sin( radians( store_latitude ) ) ) 
        )';

store.findAll({
    where:{
        [Op.or]:[{
            user_id:{[Op.in]:[
                sequelize.literal('(SELECT `Follows`.receiver_id FROM `follows` AS `Follows` WHERE `Follows`.user_id='+user_id+' and `Follows`.status="accept")')]}},
                {user_id:user_id}
                             ]
         },

    attributes: { include : [[sequelize.literal(query),'distance']]},
    where: sequelize.where(sequelize.literal(query), '<=',25),
    order: sequelize.col('distance'),
    limit: 15,
    offset: 0
})

不幸的是,您不能在WHERE或HAVING语句中使用别名字段,只能在ORDER BY中使用。您必须在WHERE子句中重复您的语句,而不要使用别名(正如SQL Use alias in Where statement中所述。)

也请访问此进行更多说明:Sequelize complex aggregate attribute in where and order