0

I have an dynamic array of id which is passed by frontend. I need to fetch data from mysql db where the tuples ids are match with the ids which i pass.

Example Id array: [6, 7, ...]

The number of items can be vary according to different requests.

SELECT expense_type, expense_category FROM expense_type WHERE expense_category_id=id1 OR expense_category_id=id2;

id1, id2 are the ids which comes from the frontend. (6,7,...)

There can be multiple ids which comes from the frontend. could i please know is there a way to fetch data from one query in such situation. Or else do i have to write multiple queries to execute?

1
  • There are two way. First you can use IN('1','2','3'). Second way you can insert these values in a temporary table and by using JOIN with that temporary table you can get result. Commented Nov 8, 2019 at 9:06

3 Answers 3

1

You need to make query by using that multiple ids's array.

something like below

query = 'SELECT expense_type, expense_category FROM expense_type WHERE expense_category_id IN (`;
forEach(ids as id) {
 query += id + ','
}

run query..

Sign up to request clarification or add additional context in comments.

Comments

0

To select data based on an array of values you can use the IN-keyword. In your case it would look like this:

SELECT expense_type, expense_category
FROM expense_type
WHERE expense_category_id IN ('id1', 'id2');

Comments

0

you can easily use an IN. also try to use Join. so the accuracy of the query will get increase.

SELECT expense_type, expense_category
FROM expense_type
WHERE expense_category_id IN ('id1', 'id2');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.