首页 > 解决方案 > Like Statement 在续集中对我不起作用

问题描述

你在做什么?

/* 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。我尝试同时使用likeand $like,但都没有工作。如果我添加where: { id: 53 },则该WHERE子句确实出现。

标签: mysqlsqlsequelize.jssql-like

解决方案


const Op = Sequelize.Op;在文件开头添加。

有两种使用like的方法。$like: [Op.like].

还可以尝试使用硬编码值来查看您的数据是否正常。


推荐阅读