Oce*_*uto 1 sql node.js sequelize.js
我的应用程序中有一个人名的搜索框.候选人的姓名存储为firstName,然后存储为lastName.当我搜索应用程序时,应用程序输入提交对ajax函数的调用,其中我有这段代码.
filters.where = {
$or: ['firstName', 'lastName', 'email'].map((item) =>
({[item]: {[queryClause]: `%${query}%`}}))
};
const scope = req.query.list;
const candidates = await CandidateModel.scope(scope).findAll(filters);
Run Code Online (Sandbox Code Playgroud)
因此,如果我输入搜索框"John",它将找到候选人,如果我输入单词"Smith",它将找到候选人.
问题是,如果我输入全名"John Smith",它将不会出现,因为查询正在检查"John Smith"是否等于"John",即名字,或"John Smith"等于"史密斯",姓氏.它不等于这些中的任何一个.
有没有办法通过sequalize中的组合字段进行过滤,因此它会测试查询是否与firstName和lastName字段组合在一起?
使用类似下面的内容在此表单中搜索全名"firstName lastName"
Sequelize.where(Sequelize.fn("concat", Sequelize.col("firstName"), ' ', Sequelize.col("lastName")), {
$ilike: '%john smith%'
})
Run Code Online (Sandbox Code Playgroud)
通过这样做,你解决了名字或姓氏可能有空格的问题.
| 归档时间: |
|
| 查看次数: |
1383 次 |
| 最近记录: |