0

Say I have an array with IDs:

values = [0, 43, 5]

Now I want to go through my table "tbl_EVENTS" and return every row where EVENTID = 0, 43, or 5.

Every ID will only appear once. So I could technically do three different queries.

I am working with a javascript frameworks that builds an SQL string in the end, so I could wrap this into a for loop if necessary.

1 Answer 1

2

This what you are looking for:

If your values = [0, 43, 5] an array then, we can use join method with template literals.

const values = [0, 43, 5];
`SELECT * FROM tbl_EVENTS WHERE EVENTID IN(${values.join(',')})`
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. But this isnt dynamic enough. I dont know the arrays length at first...
Updated my answer. No need to know the length of the array.
We are using template literals ( backticks ) here to concatenate the query and our spread.
Here is my syntax: var arrObject = new Array(); for (var i = 0; i < eventIdResults.length; i++) { arrObject.push(eventIdResults.itemAtIndex(i).tblEventID) } var string = "SELECT * FROM 10561_12865_tblEvents WHERE tblEventID IN(${arrObject.join(',')})" var res = K.query(string,[]); But it fails. Stating that I have an error in my syntax "near arrObject.join(',')"
` "SELECT * FROM 10561_12865_tblEvents WHERE tblEventID IN(${arrObject.join(',')})"` Wrap this in backticks not double quotes
|

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.