就像Statement在续篇中对我不起作用

时间:2019-01-31 08:48:39

标签: mysql sql sequelize.js sql-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]]        
    }));

您期望发生什么?

我希望续集会生成此查询:

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

即使其他子句类似WHEREORDER BY等,也不会将LIMIT条件添加到查询中。我尝试同时使用like和{{ 1}},但两者均无效。如果我添加$like,则会出现where: { id: 53 }子句。

1 个答案:

答案 0 :(得分:2)

在文件开始处添加const Op = Sequelize.Op;

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

也可以尝试使用硬编码值,以查看数据是否正常。