gorm 动态拼接查询条件
Posted haima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gorm 动态拼接查询条件相关的知识,希望对你有一定的参考价值。
结构体
type Mould struct {
MouldId string `grom:"column:mouldID"`
MouldInteriorID string `grom:"column:mouldInteriorID"`
MouldName string `grom:"column:mouldName"`
ProjectInteriorID string `grom:"column:projectInteriorID"`
projectName string `grom:"column:projectName"`
ProjectId string `grom:"column:projectID"`
}
func GetMouldAll(mouldInteriorID string,projectInteriorID string)(moulds []Mould, err error){
var sql bytes.Buffer
sql.WriteString("select projectName,mouldInteriorID,mouldName,mould.mouldID,mould.projectID,projectInteriorID from mould ")
sql.WriteString("inner join Project on Project.projectID = mould.projectID where 1 = 1 ")
if mouldInteriorID != ""{
sql.WriteString(" and mould.mouldInteriorID=‘")
sql.WriteString(mouldInteriorID)
sql.WriteString("‘")
}
if projectInteriorID!="" {
sql.WriteString(" and projectInteriorID=‘")
sql.WriteString(projectInteriorID)
sql.WriteString("‘")
}
rows,err := MsDb.Raw(sql.String()).Rows()
if err != nil{
panic(err)
}
for rows.Next(){
mould := Mould{}
err = rows.Scan(&mould.projectName,&mould.MouldInteriorID,&mould.MouldName,&mould.MouldId,&mould.ProjectId,&mould.ProjectInteriorID)
moulds = append(moulds, mould)
}
rows.Close()
return
}
以上是关于gorm 动态拼接查询条件的主要内容,如果未能解决你的问题,请参考以下文章