简体   繁体   English

Sequelize - 不区分大小写,如

[英]Sequelize - case-insensitive like

How can I achieve this in Sequelize?如何在 Sequelize 中实现这一目标?

SELECT * FROM table where lower(column) LIKE ('abcd%');

I can't find a way to mix lower function with $like我找不到将功能与$like混合的方法

You should use Sequelize.Op :你应该使用 Sequelize.Op :

Table.findAll({
    where: {
        name: {
            [Sequelize.Op.iLike]: searchQuery
        }
    }
})

Don't forget to add % before or after your searchQuery, if you want to make a partial query.如果要进行部分查询,请不要忘记在 searchQuery 之前或之后添加 %。

See the docs here请参阅此处的文档

I found the solution:我找到了解决方案:

Table.findAll({
  attributes: ['createdAt', 'col'],
  where: {
    $and:[
      {
        createdAt:{
          $between:[minDate, maxDate]
        }
      },
      Sequelize.where(
        Sequelize.fn('lower', Sequelize.col('col')),
        {
          $like: 'abcd%'
        }
      )
    ]
  }
});

If you're using PostGres, you can use the $iLike operator to search rows (NOT column names like your question asks).如果您使用的是 PostGres,则可以使用 $iLike 运算符来搜索行(不是您的问题所要求的列名)。

Sequelize Docs 续集文档

While it doesn't fully address your question, hopefully it will help someone down the road who searches for case-insensitive + Sequelize, which brought me here.虽然它没有完全解决您的问题,但希望它能帮助那些搜索不区分大小写 + Sequelize 的人,这让我来到这里。

Table.findAll({
    where: {
        createdAt: {
            $between: [minDate, maxDate]
        },
        someOtherColumn: {
            $like: '%mysearchterm%'
        }
    }
})

I run into similar problem and solved by我遇到了类似的问题并解决了

const values = ['adcd'].map(x => x.toLowerCase());

const results = await SomeModel.findAll({
   attributes: [
      ...Object.keys(SomeModel.rawAttributes),
      [Sequelize.fn('LOWER', Sequelize.col('someColumn')), 'lower'],
   ],
   having: { lower: values, },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM