SavceBoyz
SavceBoyz

Reputation: 79

how to make a conditional in where. SEQUELIZE mysql

should fetch all teachers where the value 'search' is found in the coursesOffered array.

table teacher

name lastName dob coursesOffered

juan perez 2022/10/02 [ 'math', 'science' ]

marcos smith 2022/10/02 [ 'history', 'literature' ]

Query sequelize

const search = 'math';

const teachers = await Teacher.findAll({
    where: should fetch all teachers where the value 'search' is found in the coursesOffered array.
})

Upvotes: 0

Views: 158

Answers (1)

Emma
Emma

Reputation: 9363

You can try JSON_CONTAINS function.

// When you are doing string search, make sure to wrap the search term with "".
const teachers = await Teacher.findAll({
    where: Sequelize.fn('JSON_CONTAINS', Sequelize.col('coursesOffered'), `"${search}"`)
})

Upvotes: 1

Related Questions