选项 我有一个带有搜索栏的Web应用程序。搜索栏接受customer_id,并使用customer_id作为过滤器对redshift集群运行查询。我需要能够输入多个以逗号分隔的客户ID,然后运行按这些客户ID过滤的查询。目前,我一次只能使用1个客户ID。这是我目前使用的。任何帮助或指导表示赞赏。
func riiapp(w http.ResponseWriter, r *http.Request) {
oname := r.PostFormValue("orderid")
rows, err := db.Query("SELECT rma_id, order_id, customer_id, bin_id, owner, asin, lpn, warehouse_id FROM crt.returns_in_inv WHERE (warehouse_id IN ($1) OR lower(warehouse_id) IN ($1)) OR lpn IN ($1) OR asin IN ($1) OR rma_id IN ($1) OR customer_id IN ($1) OR ORDER_ID IN ($1);", oname)
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
defer rows.Close()
bks := make([]Book, 0)
for rows.Next() {
bk := Book{}
err := rows.Scan(&bk.Rmaid, &bk.Orderid, &bk.Customerid, &bk.Binid, &bk.Owner, &bk.Asin, &bk.Lpn, &bk.Warehouseid) // order matters
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
bks = append(bks, bk)
}
if err = rows.Err(); err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
tpl.ExecuteTemplate(w, "riiapp.gohtml", bks)
fmt.Println(oname)
}
<html>
<div class="container" align="center">
<h6>Search By: Rma_Id, Order Id, Customer ID, Asin, LPN OR Warehouse ID (May Pull Many Records)</h6>
<form action="/riiapp" method="POST">
<input type="text" name="orderid" id="ord" placeholder="SEARCH">
<input type="submit">
</form>
<table id="testTable">
<thead valign="top">
<tr>
<th>Rma_id</th>
<th>Order_id</th>
<th>Customer_id</th>
<th>Bin_id</th>
<th>Owner</th>
<th>Asin</th>
<th>Lpn</th>
<th>Warehouse_id</th>
</tr>
</thead>
<tbody>{{range .}}
<tr>
<td>{{.Rmaid}}</td>
<td>{{.Orderid}}</td>
<td>{{.Customerid}}</td>
<td>{{.Binid}}</td>
<td>{{.Owner}}</td>
<td>{{.Asin}}</td>
<td>{{.Lpn}}</td>
<td>{{.Warehouseid}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</html>
答案 0 :(得分:0)
您至少有两个选择,构建一串占位符,然后在传递参数之前将其插值到查询中,或者代替使用IN
,而改用ANY
和{{3} }。
建立自己的占位符:
orderid := "44,33,55,66" // get input
ids := strings.Split(orderid, ",")
if len(ids) < 1 {
return // exit if no input
}
placeholders := ""
for i := range ids {
placeholders += ",$" + strconv.Itoa(i+1)
}
placeholders = placeholders[1:] // remove leading comma
query := `SELECT
rma_id,
order_id,
customer_id,
bin_id,
owner,
asin,
lpn,
warehouse_id
FROM crt.returns_in_inv
WHERE (warehouse_id IN (%[1]s) OR lower(warehouse_id) IN (%[1]s))
OR lpn IN (%[1]s)
OR asin IN (%[1]s)
OR rma_id IN (%[1]s)
OR customer_id IN (%[1]s)
OR ORDER_ID IN (%[1]s);`
query = fmt.Sprintf(query, placeholders) // inject placeholders $1,$2,$3,$4 into query template
rows, err := db.Query(query, ids...) // execute query
// ...
使用ANY
:
orderid := "44,33,55,66" // get input
ids := strings.Split(orderid, ",")
if len(ids) < 1 {
return // exit if no input
}
params := make(pq.Int64Array, len(ids))
for i, s := range ids {
id, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return // fail
}
params[i] = id
}
query := `SELECT
rma_id,
order_id,
customer_id,
bin_id,
owner,
asin,
lpn,
warehouse_id
FROM crt.returns_in_inv
WHERE (warehouse_id = ANY ($1) OR lower(warehouse_id) = ANY ($1))
OR lpn = ANY ($1)
OR asin = ANY ($1)
OR rma_id = ANY ($1)
OR customer_id = ANY ($1)
OR ORDER_ID IN = ANY ($1)`
rows, err := db.Query(query, params)
// ...
答案 1 :(得分:0)
//This is the code that i got to work. Thank you so much @mkopriva !!!
//You are freaking awesome!! I did have to add in 1 thing to it.
func riiapp(w http.ResponseWriter, r *http.Request) {
oname := r.PostFormValue("orderid")
ids := strings.Split(oname, ",")
if len(ids) < 1 {
return // exit if no input
}
placeholders := ""
for i := range ids {
placeholders += ",$" + strconv.Itoa(i+1)
}
placeholders = placeholders[1:] // remove leading comma
t := ids
s := make([]interface{}, len(t))
for i, v := range t {
s[i] = v
}
query := `SELECT
rma_id,
order_id,
customer_id,
bin_id,
owner,
asin,
lpn,
warehouse_id
FROM z_support.returns_in_inv
WHERE (warehouse_id IN (%[1]s) OR lower(warehouse_id) IN (%[1]s))
OR lpn IN (%[1]s)
OR asin IN (%[1]s)
OR rma_id IN (%[1]s)
OR customer_id IN (%[1]s)
OR ORDER_ID IN (%[1]s);`
query = fmt.Sprintf(query, placeholders) // inject placeholders $1,$2,$3,$4 into
query template
rows, err := db.Query(query, s...) // execute query
if err != nil {
log.Println(err)
return
}
defer rows.Close()
bks := make([]Book, 0)
for rows.Next() {
bk := Book{}
err := rows.Scan(&bk.Rmaid, &bk.Orderid, &bk.Customerid, &bk.Binid, &bk.Owner,
&bk.Asin, &bk.Lpn, &bk.Warehouseid) // order matters
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
bks = append(bks, bk)
}
if err = rows.Err(); err != nil {
http.Error(w, http.StatusText(500), 500)
return
}
tpl.ExecuteTemplate(w, "riiapp.gohtml", bks)
}