I created a postgres function that receives an array of text as parameter. For each text in the array I want to check if any of the the columns in the query match it with like clause.
Is it possible to aggregate where clause like this in a for loop?
For example for clarification, I have the above tables:
lessons
+----+-----------+-----------------+
| id | name | teacher_id |
+----+-----------+-----------------+
| 1 | English | 1 |
+----+-----------+-----------------+
| 2 | Spanish | 2 |
+----+-----------+-----------------+
Teachers
+----+-----------+
| id | name |
+----+-----------+
| 1 | Teacher 1 |
+----+-----------+
| 2 | Teacher 2 |
+----+-----------+
I want to get as parameters text[] and for each text in the array I want to execute an OR clause between the column in both tables and return array of jsons([{"id": 1, "teacher": {"id":1, "name": "Teacher1"}}])
For example if the parameters are ["lish", "er"] I want it to execute:
where
lessons.name like '%lish%' or teachers.name like '%lish%'
and
lessons.name like '%er%' or teachers.name like '%er%'
teacher with id 1 will return.
The thing is I don't know in advance the parameters so this is why I assume I need a FOR loop.
Also, how to I make the query to return an array of jsons that each teacher will be an inner json object of each lesson?
Would appreciate some examples if so. Thank you!