我尝试查询用户 ID 列表。我正在使用 Go Pq库。func ListUsers(userIDs []interface{}) (users []User, err error) { stm := `SELECT U.id, ( CASE WHEN TRIM(UP.first_name || ' ' || UP.last_name) = '' THEN U.id :: TEXT ELSE UP.first_name || ' ' || UP.last_name END ) as avatar_name,FROM users AS U JOIN user_profiles UP ON UP.user_id = U.idWHERE U.id IN ($1);` rows, err := postgresql.Instance.Query(stm, userIDs) defer rows.Close() if err != nil && err != sql.ErrNoRows { return } if err == sql.ErrNoRows { return users, nil } for rows.Next() { var ( id int AvatarName string ) err = rows.Scan(&id, &AvatarName) if err != nil { return nil, err } users = append(users, User{ID: id, AvatarName: AvatarName}) } return users, nil}当我运行此命令时,它会给出错误转换参数 $1 类型:不支持的类型 []interface {},接口切片
1 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
用pq.Array包裹切片。
更改
IN ($1)
为= any($1)
以便可以使用数组值。
这是代码:
stm := `SELECT
U.id,
(
CASE WHEN TRIM(UP.first_name || ' ' || UP.last_name) = '' THEN U.id :: TEXT ELSE UP.first_name || ' ' || UP.last_name END
) as avatar_name,
FROM
users AS U
JOIN user_profiles UP ON UP.user_id = U.id
WHERE
U.id = any($1);`
rows, err := postgresql.Instance.Query(stm, pq.Array(userIDs))
- 1 回答
- 0 关注
- 120 浏览
添加回答
举报
0/150
提交
取消