How can I execute a MySQL query using an array as an input instead of just a single value?

Viewed 104

I have a list of values like this:

[1000, 1100, 1200]

I want to execute a MySQL query that finds all of the entries in my database where a certain column is within a tolerance value (say +/- 1) of each of these values, like this:

SELECT * 
FROM test_table 
WHERE mz1_shifted
BETWEEN (1000 - 1) AND (1000 + 1)

Do I have to execute multiple queries, or can I instead pass in an array somehow:

....
BETWEEN ([1000, 1100, 1200] - 1) AND ([1000, 1100, 1200] + 1) #pseudocode
1 Answers

You would OR your conditions together.

SELECT * 
FROM test_table 
WHERE
mz1_shifted BETWEEN (1000 - 1) AND (1000 + 1)
OR
mz1_shifted BETWEEN (1100 - 1) AND (1100 + 1)
OR
mz1_shifted BETWEEN (1200 - 1) AND (1200 + 1)
Related