Like Statement 在 sequelize 中对我不起作用
Like Statement is not working for me in sequelize
你在做什么?
/* Code for Pagination */
let limit = 4; // number of records per page
let offset = 0;
let countallCompanies = 1000;
let page = req.body.page; // 1 by default page number
let order_by = req.body.order_by; // id by default
let order_by_ASC_DESC = req.body.order_by_ASC_DESC; // ASC by default
let pages = Math.ceil(countallCompanies.count / limit);
offset = limit * (page - 1);
/* End code for Pagination */
[err, companies] = await to(Company.findAll({
where: { company_name: { like: '%'+ req.body.q +'%' } },
limit: limit,
offset: offset,
order: [[order_by, order_by_ASC_DESC]]
}));
你预计会发生什么?
我希望 sequelize 生成这个查询:
SELECT * FROM `Companies` AS `Company` WHERE company_name LIKE '%aa%' ORDER BY `Company`.`id` ASC LIMIT 0, 4
实际发生了什么?
而是生成此查询:
SELECT * FROM `Companies` AS `Company` ORDER BY `Company`.`id` ASC LIMIT 0, 4
WHERE
条件未添加到我的查询中,即使其他子句如 ORDER BY
、LIMIT
等。我尝试同时使用 like
和$like
,但均无效。如果我添加 where: { id: 53 }
那么 WHERE
子句就会出现。
在文件开头添加 const Op = Sequelize.Op;
。
like有两种用法。 $like: [Op.like]
。
也可以尝试使用硬编码值来查看您的数据是否正常。
你在做什么?
/* Code for Pagination */
let limit = 4; // number of records per page
let offset = 0;
let countallCompanies = 1000;
let page = req.body.page; // 1 by default page number
let order_by = req.body.order_by; // id by default
let order_by_ASC_DESC = req.body.order_by_ASC_DESC; // ASC by default
let pages = Math.ceil(countallCompanies.count / limit);
offset = limit * (page - 1);
/* End code for Pagination */
[err, companies] = await to(Company.findAll({
where: { company_name: { like: '%'+ req.body.q +'%' } },
limit: limit,
offset: offset,
order: [[order_by, order_by_ASC_DESC]]
}));
你预计会发生什么?
我希望 sequelize 生成这个查询:
SELECT * FROM `Companies` AS `Company` WHERE company_name LIKE '%aa%' ORDER BY `Company`.`id` ASC LIMIT 0, 4
实际发生了什么?
而是生成此查询:
SELECT * FROM `Companies` AS `Company` ORDER BY `Company`.`id` ASC LIMIT 0, 4
WHERE
条件未添加到我的查询中,即使其他子句如 ORDER BY
、LIMIT
等。我尝试同时使用 like
和$like
,但均无效。如果我添加 where: { id: 53 }
那么 WHERE
子句就会出现。
在文件开头添加 const Op = Sequelize.Op;
。
like有两种用法。 $like: [Op.like]
。
也可以尝试使用硬编码值来查看您的数据是否正常。