sequelize几个常用但不好记的查询

查询指定时间

// 查询上个月的所有条目
const schemaOrderMonth = moment().subtract(1, 'months').format('YYYY-MM');
where.create_time = sequelize.literal('date_format(create_time,"%Y-%m")="'+schemaOrderMonth+'"');

查询不相等字段

// amount不等于paid
where.amount: {
     
	$ne: sequelize.col('paid')
},

同一个字段OR

// no为null或者为空
const Op = sequelize.Op;
where.no: {
      
	[Op.or]: [ null, '' ]
},

使用函数

// amount的总和
attributes: [
    [sequelize.fn('SUM', sequelize.col('amount')),'sumAmount'],
    [sequelize.fn('SUM', sequelize.col('paid')),'sumPaid']
],

数组形式的where组合查询

// content和file的模糊查询
// atme和不被软删除的字段
where: [
	$or: [
		sequelize.where(sequelize.col('content'), {
      '$like': '%'+keywords+'%'}),
		sequelize.where(sequelize.col('file'), {
      '$like': '%'+keywords+'%'}),
	],
    $and: [
		sequelize.col('isdel'), {
      $eq: 0}),
		sequelize.where(sequelize.col('atMe'), {
      $eq: 1})
	]
],

你可能感兴趣的:(mysql,orm,nodejs)