连表查询实例:两个表的数据
接收数据结构体:
type NewsAll struct {
Id int `json:"id" gorm:"column:id"`
Title string `json:"title" gorm:"column:title"`
Content string `json:"content" gorm:"column:content"`
CateId int `json:"cate_id" gorm:"column:cate_id"`
CateName string `json:"cate_name" gorm:"column:cate_name"`
}
gorm连表查询数据
var news []model.NewsAll
db := utils.Db
db.Table("news n").
Select("n.id,n.title,n.content,n.cate_id,c.cate_name").
Joins("left join cate c on n.cate_id = c.id ").Scan(&news)
c.JSON(http.StatusOK, gin.H{"list":news})
取别名方式:
返回数据: