亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

節點Js將原始查詢更改為緯度和經度的ORM

節點Js將原始查詢更改為緯度和經度的ORM

qq_遁去的一_1 2021-05-12 13:15:12
我試圖隱瞞我對sequelize的原始查詢ORM,但沒有得到正確的結果。這是我的原始查詢var query = "SELECT ( 3959 * acos( cos( radians('40.892389') ) * cos( radians( c.shopLatitude ) ) * cos( radians( c.shopLongitude ) - radians('-74.258633') ) + sin( radians('40.892389') ) * sin(radians(shopLatitude)) ) ) AS distance FROM cubbers c HAVING distance < 20 ORDER BY distance ASC";db.sequelize.query(query, { type: sequelize.QueryTypes.SELECT} ).then(dataList=>{    res.send(dataList)}).catch(error=>{    res.send('error')});ORM:CubbersShopsData.findOne({    where: { cubbersId: data.cubbersId },    include: [ CubbersAvailability ],    attributes: [[sequelize.literal("3959 * acos(cos(radians("+shopLatitude+")) * cos(radians(shopLatitude)) * cos(radians("+shopLongitude+") - radians(shopLongitude)) + sin(radians("+shopLatitude+")) * sin(radians(shopLatitude)))"),'distance']],    logging: console.log}).then(dataList=>{    res.send(dataList)}).catch(error=>{    res.send('error')});在我的ORM代碼中,我想添加orderBy和having。如果正在添加,order:['distance', 'ASC'],則顯示此錯誤Unknown column 'cubbers.distance' in 'order clause
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

不幸的是,sequelize在have和order子句中使用別名存在一些問題。因此,sequelize.literal在這種情況下我總是使用方法。


CubbersShopsData.findOne({

    where: { cubbersId: data.cubbersId },

    include: [ CubbersAvailability ],

    attributes: [[sequelize.literal("3959 * acos(cos(radians("+shopLatitude+")) * cos(radians(shopLatitude)) * cos(radians("+shopLongitude+") - radians(shopLongitude)) + sin(radians("+shopLatitude+")) * sin(radians(shopLatitude)))"),'distance']],

    having: sequelize.literal('distance < 20'),

    order: sequelize.literal('distance ASC'),

    logging: console.log

}).then(dataList=>{

    res.send(dataList)

}).catch(error=>{

    res.send('error')

});


查看完整回答
反對 回復 2021-05-27
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號