简体   繁体   中英

postgres +node.js query combine

I am new to this node.js and postgres.. I am executing 2 queries(select) and i have to combie these 2 queries output into a single result. Also i have to make the 1st result to appear first and then the 2nd query.. I have the following code but i am not achieving the requirement. Also i am not sure that the code i have is correct..Suggest other idea if you have..

My code:

  client.query("select * from tn_village where level3 ILIKE '%"+villagename+"%' OR level4 ILIKE '%"+villagename+"%' OR level5 ILIKE '%"+villagename+"%' OR level6 ILIKE '%"+villagename+"%' OR level7 ILIKE '%"+villagename+"%' OR level8 ILIKE '%"+villagename+"%' OR level9 ILIKE '%"+villagename+"%'",function(err,result)
    {
    client.query("select * from tn_village where level3 ILIKE '%"+village+"%' OR level4 ILIKE '%"+village+"%' OR level5 ILIKE '%"+village+"%' OR level6 ILIKE '%"+village+"%' OR level7 ILIKE '%"+village+"%' OR level8 ILIKE '%"+village+"%' OR level9 ILIKE '%"+village+"%'" ,function(err,result1)
    {      
    res.send(result1);
    });
    });

How can i combine result and result1 to a single output..Help me to solve this..Thanks in advance..

EDIT:

client.query("select * from tn_village where level3 ILIKE '%"+villagename+"%' OR level4 ILIKE '%"+villagename+"%' OR level5 ILIKE '%"+villagename+"%' OR level6 ILIKE '%"+villagename+"%' OR level7 ILIKE '%"+villagename+"%' OR level8 ILIKE '%"+villagename+"%' OR level9 ILIKE '%"+villagename+"%' ",function(err,result)
{
client.query("select * from tn_village where level3 ILIKE '%"+village+"%' OR level4 ILIKE '%"+village+"%' OR level5 ILIKE '%"+village+"%' OR level6 ILIKE '%"+village+"%' OR level7 ILIKE '%"+village+"%' OR level8 ILIKE '%"+village+"%' OR level9 ILIKE '%"+village+"%'" ,function(err,result1)
{ 
var ret = {
            result: result,----------->if i put result1 here only the 2nd query output is showing. If i put result only 1st query output is shown..
            result1: result1
        }    


res.send(ret); 
//res.send(result1);      
});
}); 
});

Also i want i have doubt,that whether your code will show output of 1st query first and then 2nd query output??

Well, simply following the way you have it laid out (although I prefer promises even in node for aggregating multiple async calls):

 client.query("select ...",function(err,result) {
    client.query("select ..." ,function(err,result1) {  
        var ret = {
            result: result,
            result1: result1
        }
        res.send(ret);
    });
 });

That is the simplest example I can think of to show you how easy this should be. Now, if for example you are thinking of some sort of loop over result and result1 that will combine/munge them together etc, or if you want to concatenate the two arrays into one etc, all of that isn't clear by your question but is easy to do.

Let me know if that clarifies at all. In essence the value of "result" is still available to you in the inner callback, so you can make use of it in your res.send.

PS - nesting calls that don't necessarily need to be nested is a bit derpy, promises or other callback aggregators would help here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM