Go:如何在mysql查询中使用`fmt`?

问题描述 投票:0回答:1

我使用fmt在my_table中创建两个字段。

const   (
    SELECT_ALL_USERID = "select user_id from notification_settings where ? = 1 and ? = 1 and frequency = ? and is_holiday = ?"
)

func (user_station) GetAllUserId(db *gorp.DbMap,isLine string ,isHourClock int ,frequency , isHoliday string) ([]models.UserID,error) {
    var listUserId []models.UserID
    var is_line = fmt.Sprintf("is_%s",isLine)
    var is_hour_oclock = fmt.Sprintf("is_%d_oclock",isHourClock)
    _,err := db.Select(&listUserId,SELECT_ALL_USERID,is_line,is_hour_oclock,frequency,isHoliday)
    return listUserId,err
}

因此,我在WHERE CLAUSE中尝试了?,但失败了。我想念什么?

mysql go fmt
1个回答
0
投票

您不能使用?来绑定列名,而只能绑定值。而是使用fmt构建查询字符串:

select_all_userid:=fmt.Sprintf("select user_id from notification_settings where is_%s = 1 and is_%s_oclock = 1 and frequency = ? and is_holiday = ?",isLine,isHourClock)
© www.soinside.com 2019 - 2024. All rights reserved.