SQL: Can I use an input array in where where?

Possible duplicate:
Parameterizing an SQL IN Clause?

Suppose I had a school table in which school_name, school_enrolment

As input to my program, someone enters a list of schools that they would like to enroll in. Instead of generating an SQL query, for example:

SELECT * FROM school_table WHERE school_name = 'program_input_1' or school_name = 'program_input_2' or school_name = 'program_input_3' 

maybe or just do something like

 SELECT * from school_table WHERE school name in [array of program inputs] 

how is a cleaner way to write this?

+4
source share
2 answers

Yes, this is what IN for:

 SELECT col1, col2, ..., coln FROM school_table WHERE school_name IN ('program_input_1', 'program_input_2', 'program_input_3') 
+5
source

You can use the IN(...) clause:

 WHERE School_name in ('school 1','school 2') 

In terms of array transfer, it depends on the programming language you use to generate SQL.

You will need to write a custom function that moves through the array, dynamically generating an IN clause

+1
source

Source: https://habr.com/ru/post/1446557/


All Articles