【问题标题】:Postgres select WHERE col1, col2 IN with 2d golang slicePostgres 使用 2d golang 切片选择 WHERE col1, col2 IN
【发布时间】:2020-01-12 12:30:32
【问题描述】:

我不确定如何在 2dslice 中进行 postgres 查询 where (col1, col2)

我尝试了以下方法:

`

CREATE TABLE table2 (
  id CHAR(27) NOT NULL,
  lat FLOAT8 NOT NULL,
  lon FLOAT8 NOT NULL,
  PRIMARY KEY (id)
);


latlongdata := [][]float64{}
latlongdata = append(latlongdata, []float64{1.2, 2.3},)
latlongdata = append(latlongdata, []float64{1.3, 2.4},)
......................................
latlongdata = append(latlongdata, []float64{1.4, 2.5},)

fmt.Println(latlongdata)// prints [[1.2 2.3] [1.3 2.4] ....... [1.4 2.5]] (very long array)

Query: r.db.QueryContext(ctx,("SELECT id, lat, lon FROM table2 WHERE (lat, lon) IN $1", latlongdata,)

`

你能建议怎么做吗?

【问题讨论】:

标签: postgresql go


【解决方案1】:
Query: r.db.QueryContext(ctx,("SELECT id, lat, long FROM table2 WHERE lat, long IN (select $1, $2 union select $3, $4)", latlongdata[0][0], latlongdata[0][1], latlongdata[1][0], latlongdata[1][1])

或创建 latlongdata 并将其插入到临时表中 那么

WHERE lat, long IN (select filed1, field2 from temptable)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2011-06-05
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多