How to execute an IN lookup in SQL using Golang?

前端 未结 9 945
余生分开走
余生分开走 2020-12-01 02:12

What does Go want for the second param in this SQL query. I am trying to use the IN lookup in postgres.

stmt, err := db.Prepare(\"SELECT * FRO         


        
9条回答
  •  旧巷少年郎
    2020-12-01 02:31

    //I tried a different way. A simpler and easier way, maybe not too efficient.
    stringedIDs := fmt.Sprintf("%v", ids)
    stringedIDs = stringedIDs[1 : len(stringedIDs)-1]
    stringedIDs = strings.ReplaceAll(stringedIDs, " ", ",")
    query := "SELECT * FROM users WHERE id IN ("  + stringedIDs + ")"
    //Then follow your standard database/sql Query
    rows, err := db.Query(query)
    //error checking
    if err != nil {
        // Handle errors
    } else {
        // Process rows
    }
    

提交回复
热议问题