跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 伸手求一个 gorm 的查询方法
未分類
27 10 月 2020

伸手求一个 gorm 的查询方法

伸手求一个 gorm 的查询方法

資深大佬 : zxCoder 2

看了一上午文档还没写出来

post tag post_tag_relation 三个表,post 和 tag 是多对多关系

现在要从关系表中查找某个 tagId 对应的所有 postId,再查这些 postId 对应的 post 信息,就包括把每个 post 对应的所有 tag 也查出来,有什么方便的方法吗

因为查询条件不确定,所以目前是这样写的

tagId,ok:=para["tagId"]  if ok{   tagId=int(tagId.(float64))   var postIds []int64   var rels []models.PostTagRelation   err := db.Table("post_tag_relation").Where("tag_id=?", tagId).Where("del=?",0).Select("postId").Find(&rels).Error   if err != nil {    log.Println("query error: ", err)    c.JSON(200, failResp)    return   }   for _, rel := range rels {    postIds = append(postIds, rel.PostId)   }   db2=db2.Where("id in (?)",postIds)  } 

大佬有話說 (0)

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具