mysql当查询条件为空时不作为条件查询(Gorm)

内容纲要
// Show 查询
func (p *CategoryModel) Show(parent interface{}) (counts int64, temp []CategoryModel) {
    if parent == 0 {
        parent = sql2.NullInt32{}
    }
    sql := "SELECT * FROM tb_category WHERE (? IS NULL OR parent_id = ?);"
    if res := p.Raw(sql, parent, parent).Find(&temp); res.RowsAffected > 0 {
        return int64(len(temp)), temp
    }
    return 0, nil
}