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
}
[Haima的博客]
http://www.cnblogs.com/haima/