Wednesday, 17 May 2017

how may i return a string field from a select statement in sql

i want to return a string field in this query selection.. for example i want to add sth like daughter when daughter query is retrieved; something like this:

[{ 
"relationship_type": "parent", 
"org_name": "Banana tree" 
}, { 
"relationship_type": "parent", 
"org_name": "Big banana tree" 
}, { 
"relationship_type": "sister", 
"org_name": "Brown Banana" 
}, { 
"relationship_type": "sister", 
"org_name": "Green Banana" 
}, { 
"relationship_type": "daughter", 
"org_name": "Phoneutria Spider" 
}, { 
"relationship_type": "sister", 
"org_name": "Yellow Banana" 
}]

from this code:

app.get("/api/listAll/all/:id", function(req, res){
       var daughtersQuery = "select * from organization  where parent_id = " + req.params.id;
       var parentQuery = "select * from organization  where id = (select parent_id from organization where id = " + req.params.id + ")";   
       var sistersQuery = "select * from organization  where parent_id = (select parent_id from organization where id = " + req.params.id + ") && id != " + req.params.id;

       var q = "select distinct id, org_name, parent_id from (" +
                        daughtersQuery + " union all " +
                        parentQuery + " union all " +
                        sistersQuery + " ) a order by org_name asc";
      //  console.log(q);
         connection.query(q, function (error, results) {
              if (error) throw error;
          // console.log(results);
          res.send(results);
        });
       //  var page_no = req.query.page;
        // console.log(page_no);
});

app.listen(8080, function() {
    console.log("Server Running");
});

am currently having something like this:

[
  {
    "id": 1,
    "org_name": "black banana",
    "parent_id": null
  },
  {
    "id": 3,
    "org_name": "green banana",
    "parent_id": 2
  }
]



via giresse

No comments:

Post a Comment