猿问

我想按距离对数据进行排序,但失败了

我想按距离对数据进行排序,但失败了。


表位


type Place struct 

{

    gorm.Model

    CountyName    string 

    Name          string  

    Address       string 

    ServiceTime   string 

    Phone         string  

    BusinessScope string  

    Remark        string 

    Lng           float64

    Lat           float64 

    Type          uint 

}

这有效:


func PlaceList(placeType, fieldName, fieldValue string, pageNum, pageSize int) (places []Place, err error) {

    result := DB.Scopes(Paginate(pageNum, pageSize)).Scopes(Filter(placeType, fieldName, fieldValue)).Find(&places)

    return places, result.Error

}

我试图对它进行排序,但这不起作用:


func PlaceList(currentLng, currentLat, placeType, fieldName, fieldValue string, pageNum, pageSize int) (places []Place, err error) {

    var distance = "places.lng, places.lat, power((%s - places.lng),2) + power((%s - places.lat),2) as distance"

    var selectDistance  = fmt.Sprintf(distance, currentLng, currentLat)

    result := DB.Table("places").Select(selectDistance).Scopes(Paginate(pageNum, pageSize)).Scopes(Filter(placeType, fieldName, fieldValue)).Order("distance").Find(&places)

    return places, result.Error

}


梵蒂冈之花
浏览 120回答 1
1回答

有只小跳蛙

现在没事了func PlaceList(currentLng, currentLat, placeType, fieldName, fieldValue string, pageNum, pageSize int) (places []Place, err error) {    var distance = "*, power((%s - places.lng),2) + power((%s - places.lat),2) as distance"    var selectDistance = fmt.Sprintf(distance, currentLng, currentLat)    result := DB.Debug().Table("places").Select(selectDistance).Scopes(Paginate(pageNum, pageSize)).Scopes(Filter(placeType, fieldName, fieldValue)).Order("distance").Find(&places)    return places, result.Error}
随时随地看视频慕课网APP

相关分类

Go
我要回答