【问题标题】:how to make a conditional in where. SEQUELIZE mysql如何在 where 中设置条件。序列化mysql
【发布时间】:2022-11-14 08:34:05
【问题描述】:

应该获取在 coursesOffered 数组中找到值“搜索”的所有教师。

表老师

姓名姓氏dob课程提供

胡安·佩雷斯 2022/10/02 [“数学”、“科学”]

马科斯·史密斯 2022/10/02 [ '历史', '文学' ]

查询续集

const search = 'math';

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

【问题讨论】:

  • coursesOffered 列是什么 SQL 数据类型?
  • 该列的数据类型为 JSON - DataTypes.JSON
  • Here 你可以找到正确的语法。这个问题很老而且仍然存在,也许有一些错误。你还是可以试试的。如果使用 sequelize 语法不起作用,您仍然可以构建原始 SQL 查询
  • 我收到此语法错误

标签: javascript mysql node.js sequelize.js


【解决方案1】:

你可以试试JSON_CONTAINS 功能。

// 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}"`)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多