【发布时间】:2018-12-05 11:06:35
【问题描述】:
我正在尝试使用GORM 提出特定请求。
这是我使用的表格:
+-------------------+
| 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)"`
}
商店表
+-------------+------------------+------+-----+---------+----------------+
| 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,
})
}
所以我们有Stores,其中有Locations,我们希望在我们的Location 15 公里范围内显示Stores .只有最后一个位置有用,因此我们将结果按降序限制为 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
【问题讨论】:
-
我对这里的语法感到困惑:
..., shop_id ( ...-- 这意味着shop_id是某种函数。然而( ... ) as distanct是有道理的。shop_id之后可能缺少逗号? -
Order("locations.id DESC").Limit(1)谈到了id,还有另外两件事谈到了distance。 -
您好,谢谢您的留言,如果您看表的结构,商店有位置,它们是定期更改位置的商店。
-
还有关于
shop_id的问题? -
<opinion>Alas,我对 ORM 一无所知。不幸的是,大多数第 3 方软件包尝试将程序员与 SQL 隔离开来,程序员仍然需要学习 SQL 才能完成琐碎查询以外的任何事情。</opinion>所以,我正在接近你的问题从“这里是 SQL,现在弄清楚如何将其逆向工程为 ORM。