作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 SQL Server SELECT 查询,我想将它转换为 Sequelize 查询生成器:
SELECT ...
FROM usertbl as user
WHERE
[user].[is_deleted] = 0
AND [user].[id_role] = 6
AND ( LOWER([user].[cardid_print]) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].code) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].[firstname]) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].lastname) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].fullnameEN) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].firstname) LIKE CONCAT('%', query_string, '%')
OR LOWER([user].idnumber) LIKE CONCAT('%', query_string, '%')
OR CONCAT(LOWER([user].lastname), ' ', LOWER([user].firstname)) LIKE CONCAT('%', query_string, '%')
)
{
where: {
is_deleted: 0,
...
}
}
{
where: sequelize.where(sequelize.fn('FUNCTION',
sequelize.col('field')), 'value')
}
最佳答案
你可以试试吗?并让我知道:)
var sequelize = require('sequelize');
var query_string = 'something..';
var id_role = 6;
var query = {
is_deleted: 0
}
if(id_role !== undefined){
query['id_role'] = id_role;
}
if(query_string !== undefined || query_string.length <= 0){
query['$or'] = sequelize.literal(`( LOWER(cardid_print) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(code) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(firstname) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(lastname) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(fullnameEN) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(firstname) LIKE CONCAT('%', ${query_string}, '%')
OR LOWER(idnumber) LIKE CONCAT('%', ${query_string}, '%')
OR CONCAT(LOWER(lastname), ' ', LOWER(firstname)) LIKE CONCAT('%', ${query_string}, '%')
)`);
}
User.findAll({
where: query
})
.then(users => {
})
.catch(err => {
console.log('err', err);
});
关于node.js - 如何在 sequelize 查询中编写这个 Where 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55094358/
我是一名优秀的程序员,十分优秀!