使用SQLBoiler和Golang进行类似于MYSQL的操作。

huangapple go评论87阅读模式
英文:

Like operation MYSQL using SQLBoiler and golang

问题

我想在使用SQL boiler和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操作不起作用。

数据可以获取,但是LIKE操作不起作用,无法获取使用电子邮件或屏幕名称进行筛选的数据。

筛选不起作用。

英文:

I want to Perform LIKE operation in MYSQL using SQL boiler and golang

I am using

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%"}}}  

Here Like is not working.

Data is getting, but like operation not working, that is not getting the data that filter using the email or screenname.

filtering not working

答案1

得分: 0

我得到了答案。

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+"%"))
}
英文:

I got the Answer.

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+"%"))
	}

huangapple
  • 本文由 发表于 2021年7月21日 23:09:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/68472044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定