我正试着向戈姆提出一个具体的要求。
以下是我使用的表格:
+-------------------+
| Tables |
+-------------------+
| locations |
| shops |
| shops_tags |
| tags |
+-------------------+
位置表
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| shop_id | bigint(20) | YES | | NULL | |
| lat | decimal(10,8) | YES | | NULL | |
| lng | decimal(11,8) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
位置模型
type Location struct {
gorm.Model
ShopID int64
Shop Shop
Lat float64 `gorm:"type:decimal(10,8)"`
Lng float64 `gorm:"type:decimal(11,8)"`
}
商店table
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(180) | YES | UNI | NULL | |
| active | tinyint(1) | YES | | 1 | |
+-------------+------------------+------+-----+---------+----------------+
商店模型
type Shop struct {
gorm.Model
Name string `json:"name" gorm:"type:varchar(180);unique_index"`
Active int `json:"active" gorm:"type:tinyint(1);default:1"`
Tags []*Tag `json:"tags" gorm:"many2many:shops_tags;"`
Locations []Location `json:"locations" gorm:"locations"`
}
标签型号
type Tag struct {
gorm.Model
Name string `json:"name" gorm:"type:varchar(180)"`
Shops []*Shop `json:"shops" gorm:"many2many:shops_tags;"`
}
返回json查询的处理程序
func GetShops(c echo.Context) error {
db := db.DbManager()
// Get POST data (name lat and lng)
type geo struct {
Lat string `json:"lat" form:"lat" query:"lat"`
Lng string `json:"lng" form:"lng" query:"lng"`
}
// Bind request body
g := new(geo)
if err := c.Bind(g); err != nil {
return c.JSON(http.StatusForbidden, err)
}
shops := []model.Shop{}
// Totally disordered attempt of a query with `go-gorm` to display the list of nearby shops, sorted by the distance between us.
db.Preload("Locations", func(db *gorm.DB) *gorm.DB {
// How here use g.Lat and g.Lng for this request
return db.Order("locations.id DESC").Limit(1)
}).Preload("Tag").Find(&shops)
// Json
return c.JSON(http.StatusOK, echo.Map{
"shops": &shops,
})
}
所以我们有商店,有位置,我们想在距离我们位置15公里的范围内展示商店。只有最后一个位置是有用的,这就是为什么,我们将结果按降序限制为1。
编辑
感谢@rick james建议我重新表述我的问题,下面是中的问题 MYSQL
,如何适应 go-gorm
:
SELECT
shops.id,
shops.name, (
(
6371.04 * ACOS(
(
(
COS(
(
(
PI() / 2
) - RADIANS(
(90 - locations.lat)
)
)
) * COS(
PI() / 2 - RADIANS(90 - -33.73788500)
) * COS(
(
RADIANS(locations.lng) - RADIANS('151.23526000')
)
)
) + (
SIN(
(
(
PI() / 2
) - RADIANS(
(90 - locations.lat)
)
)
) * SIN(
(
(
PI() / 2
) - RADIANS(90 - -33.73788500)
)
)
)
)
)
)
) AS 'distance',
locations.id AS 'location_id',
locations.shop_id,
locations.lat,
locations.lng,
locations.created_at
FROM
shops
INNER JOIN locations ON(
locations.created_at >= '2017-12-13'
AND(
(
6371.04 * ACOS(
(
(
COS(
(
(
PI() / 2
) - RADIANS(
(90 - locations.lat)
)
)
) * COS(
PI() / 2 - RADIANS(90 - -33.73788500)
) * COS(
(
RADIANS(locations.lng) - RADIANS('151.23526000')
)
)
) + (
SIN(
(
(
PI() / 2
) - RADIANS(
(90 - locations.lat)
)
)
) * SIN(
(
(
PI() / 2
) - RADIANS(90 - -33.73788500)
)
)
)
)
)
)
) < '500'
AND shops.id = (locations.shop_id)
)
WHERE
shops.active = 1
GROUP BY
shops.id
ORDER BY
distance ASC
LIMIT
100
1条答案
按热度按时间btxsgosb1#
看看这个查询,我肯定会手工创建这个查询,而不是依赖任何orm来执行查询。请参阅在gorm数据库示例ref上使用exec方法。
但是。。。要使用像gorm这样的orm,可以使用gorm的sql生成器。这是一个应该具有指导意义的例子。请注意,这是针对sqlite数据库的,而不是针对mysql的,因此所使用的函数不存在。我们主要关心的是构建查询。
使用sql builder生成的查询