简体   繁体   中英

Is there a way to push the results of a PostgreSQL query into a JavaScript array?

Just looking to see if there's an elegant solution to this problem:

Is there a way to loop through the results of a psql query and return a specific result based on the SQL query?

For example, let's say I wanted to SELECT amount_available FROM lenders ORDER BY interest_rate , and I wanted to loop through the column looking for any available amounts, add those available amounts to a variable, and then once that amount reached a certain figure, exit.

More verbose example:

Let's say I have someone who wants to borrow $400. I want to go through my lenders table, and look for any lender that has available funds to lend. Additionally, I want to start looking at lenders that are offering the lowest interest rate. How could I query the database and find the results that satisfy the $400 loan at the lowest interest rate, and stop once I've reached my goal, instead of searching the whole db? And can I do that inside a JavaScript function, returning those records that meet that criteria?

Maybe I'm trying to do something that's not possible, but just curious.

Thanks!

You translate your requirement into the SQL language. After all, SQL is a descriptive language. The database engine then figures out how to process the request.

Your example sound like

SELECT name
FROM lenders
WHERE property >= 400
ORDER BY interest_rate
FETCH FIRST ROW ONLY;

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