node-postgres: how to execute “WHERE col IN ()” query?

后端 未结 7 605
梦毁少年i
梦毁少年i 2020-11-28 06:02

I\'m trying to execute a query like this:

SELECT * FROM table WHERE id IN (1,2,3,4)

The problem is that the list of ids I want to filter ag

7条回答
  •  生来不讨喜
    2020-11-28 06:11

    Another posible solution is for example for REST API in NODE JS:

    var name = req.body;//Body is a objetc that has properties for example provinces
    var databaseRB = "DATABASENAME"
    var conStringRB = "postgres://"+username+":"+password+"@"+host+"/"+databaseRB; 
    
    var filter_query = "SELECT row_to_json(fc) FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json((parameters) As properties FROM radiobases As lg WHERE lg.parameter= ANY($1) )As f) As fc";
    
    var client = new pg.Client(conStringRB);
    client.connect();
    var query = client.query(new Query(filter_query,[name.provinces]));
    query.on("row", function (row, result) {
      result.addRow(row);
    });
    query.on("end", function (result) {
     var data = result.rows[0].row_to_json
       res.json({
         title: "Express API",
         jsonData: data
         });
    });
    

    Keep in mind that any type of array can be used

提交回复
热议问题