sequelize 操作符

1 sequelize 操作符

const {
    
     Op } = require("sequelize");
Post.findAll({
    
    
  where: {
    
    
    [Op.and]: [{
    
     a: 5 }, {
    
     b: 6 }],            // (a = 5) AND (b = 6)
    [Op.or]: [{
    
     a: 5 }, {
    
     b: 6 }],             // (a = 5) OR (b = 6)
      
    某个属性: {
    
    
      // 基本
      [Op.eq]: 3,                              // = 3
      [Op.ne]: 20,                             // != 20
      [Op.is]: null,                           // IS NULL
      [Op.not]: true,                          // IS NOT TRUE
      [Op.or]: [5, 6],                         // (someAttribute = 5) OR (someAttribute = 6)

      // 使用方言特定的列标识符 (以下示例中使用 PG):
      [Op.col]: 'user.organization_id',        // = "user"."organization_id"

      // 数字比较
      [Op.gt]: 6,                              // > 6
      [Op.gte]: 6,                             // >= 6
      [Op.lt]: 10,                             // < 10
      [Op.lte]: 10,                            // <= 10
      [Op.between]: [6, 10],                   // BETWEEN 6 AND 10
      [Op.notBetween]: [11, 15],               // NOT BETWEEN 11 AND 15

      // 其它操作符

      [Op.all]: sequelize.literal('SELECT 1'), // > ALL (SELECT 1)

      [Op.in]: [1, 2],                         // IN [1, 2]
      [Op.notIn]: [1, 2],                      // NOT IN [1, 2]

      [Op.like]: '%hat',                       // LIKE '%hat'
      [Op.notLike]: '%hat',                    // NOT LIKE '%hat'
      [Op.startsWith]: 'hat',                  // LIKE 'hat%'
      [Op.endsWith]: 'hat',                    // LIKE '%hat'
      [Op.substring]: 'hat',                   // LIKE '%hat%'
      [Op.iLike]: '%hat',                      // ILIKE '%hat' (不区分大小写) (仅 PG)
      [Op.notILike]: '%hat',                   // NOT ILIKE '%hat'  (仅 PG)
      [Op.regexp]: '^[h|a|t]',                 // REGEXP/~ '^[h|a|t]' (仅 MySQL/PG)
      [Op.notRegexp]: '^[h|a|t]',              // NOT REGEXP/!~ '^[h|a|t]' (仅 MySQL/PG)
      [Op.iRegexp]: '^[h|a|t]',                // ~* '^[h|a|t]' (仅 PG)
      [Op.notIRegexp]: '^[h|a|t]',             // !~* '^[h|a|t]' (仅 PG)

      [Op.any]: [2, 3],                        // ANY ARRAY[2, 3]::INTEGER (仅 PG)
      [Op.match]: Sequelize.fn('to_tsquery', 'fat & rat') // 匹配文本搜索字符串 'fat' 和 'rat' (仅 PG)

      // 在 Postgres 中, Op.like/Op.iLike/Op.notLike 可以结合 Op.any 使用:
      [Op.like]: {
    
     [Op.any]: ['cat', 'hat'] }  // LIKE ANY ARRAY['cat', 'hat']
    }
  }
});

2 Op.and

const users = await User.findAll({
    
     
    where: {
    
     
		[Op.and]:[
            {
    
     name: '小徐'},
            {
    
     age: 18 }
        ]
  	}
})
// select * from User where name = '小徐' and age = 18;

3 Op.or

const users = await User.findAll({
    
     
    where: {
    
     
		[Op.or]:[
            {
    
     age: 18 },
            {
    
     age: 19 }
        ]
  	}
})

const users = await User.findAll({
    
     
    where: {
    
     
		age: {
    
    
            [Op.or]:[18,19]
        }
  	}
})

// select * from User where age = 18 or age = 19;

4 Op.in

const users = User.findAll({
    
    
    where: {
    
    
        id: {
    
    
            [Op.in]:[1,2,3]
        }
    }
})
// 上下等效
const users = User.findAll({
    
    
    where: {
    
    
        id:[1,2,3]
    }
})

猜你喜欢

转载自blog.csdn.net/qq_53931766/article/details/125377808