【问题标题】:Like operation MYSQL using SQLBoiler and golang使用SQLBoiler和golang来like操作MYSQL
【发布时间】:2021-07-21 15:09:29
【问题描述】:

我想使用 SQL 锅炉和 golang 在 MYSQL 中执行 LIKE 操作

我正在使用

github.com/volatiletech/sqlboiler/v4/queries/qm

.

clause = qm.Where(fmt.Sprintf("post.deleted_at is null"))
    queryMods := []qm.QueryMod{
        clause,
        qm.Offset(gpi.Offset),
        qm.Limit(gpi.Limit),
        orderByMod,
        qm.Load(dbmodels.PostRels.ImpartWealth), // the user who posted
    }
    if searchKey != "" {
    where := fmt.Sprintf(`user on user.impart_wealth_id=post.impart_wealth_id 
and user.screen_name like ? or user.email like ? `)
queryMods = append(queryMods, qm.InnerJoin(where, "%"+searchKey +"%", "%"+searchKey +"%"))
    }
    posts, err := dbmodels.Posts(queryMods...).All(ctx, m.db)



[]qm.QueryMod{qmhelper.WhereQueryMod{Clause:"post.deleted_at is null",
 Args:[]interface {}(nil)}, 
qm.offsetQueryMod{offset:0},
 qm.limitQueryMod{limit:1}, 
qm.orderByQueryMod{clause:"created_at desc, post_id desc"},
qm.loadQueryMod{relationship:"ImpartWealth", mods:[]qm.QueryMod(nil)}, 
qm.innerJoinQueryMod{clause:"user on user.impart_wealth_id=post.impart_wealth_id \n\t\tand user.screen_name like ? or user.email like ? ",
 args:[]interface {}{"%j%", "%j%"}}}  





  

“点赞”不起作用。

数据正在获取,但就像操作不起作用一样,即没有获取使用电子邮件或屏幕名称过滤的数据。

过滤不工作

【问题讨论】:

  • 你确定恐慌是由like引起的吗? Go 中的恐慌通常会输出堆栈跟踪,您可以在问题中包含它吗?
  • @mkopriva,我更新了错误..我是新手..如果我删除里面的代码 if searchKey != "" this
  • show fmt.Printf("%#v\n",queryMods),我认为里面有些东西是零。
  • @MiaMia 你的第一个模组是nil,好像是clause
  • @MiaMia 也不会将大量代码 sn-ps 发布到 cmets 中,它们很难阅读。您可以通过添加详细信息和适当的语法高亮来多次编辑问题。

标签: go sqlboiler


【解决方案1】:

我得到了答案。

if gpi.SearchKey != "" {
        where := fmt.Sprintf(`user on user.impart_wealth_id=post.impart_wealth_id 
        and (user.screen_name like ? or user.email like ? ) `)
        queryMods = append(queryMods, qm.InnerJoin(where, "%"+gpi.SearchKey+"%", "%"+gpi.SearchKey+"%"))
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2019-09-05
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多