如果不为null,则在哪里续集

Asf*_*sdf 1 javascript sql node.js express

可以说我想用

WHERE ID=2134

但是,如果用户不提供该ID,则不应使用WHERE ID(因为它为null)

如何使用Sequelize处理此问题?

小智 11

Post.update({
  updatedAt: null,
}, {
  where: {
    deletedAt: {
      [Op.ne]: null
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 使用“[Op.ne]”与“[Op.not]”之间有区别吗? (12认同)
  • 不确定,但我认为 SQL 中的“[Op.ne]”是“!”,而“[Op.not]”是“NOT”。 (3认同)

小智 10

我读了标题,上面写着,如果不是空的。但是,我显示 IS NOT NULL 和 IS NULL

我附上一个例子。

    const cars = await Car.findAll({
    where: {
        userId: {
            [Op.in]: myUserIds, // array like userId IN [2, 3, 4]
        },
        action: 'start', // like: action = 'start'
        sellDate: {
            [Op.not]: null, // Like: sellDate IS NOT NULL
        },
        status: {
            [Op.is]: null, // Like: status IS NULL
        }
    },
    order: [
        ['id', 'DESC'] // Like: ORDER BY id DESC
    ],
    limit: 5, 
    offset: 1
});
Run Code Online (Sandbox Code Playgroud)

有关详细信息,我附上了文件 node_modules/sequelize/types/lib/operators.d.ts

我希望能帮到你。

    const cars = await Car.findAll({
    where: {
        userId: {
            [Op.in]: myUserIds, // array like userId IN [2, 3, 4]
        },
        action: 'start', // like: action = 'start'
        sellDate: {
            [Op.not]: null, // Like: sellDate IS NOT NULL
        },
        status: {
            [Op.is]: null, // Like: status IS NULL
        }
    },
    order: [
        ['id', 'DESC'] // Like: ORDER BY id DESC
    ],
    limit: 5, 
    offset: 1
});
Run Code Online (Sandbox Code Playgroud)


Yur*_*gen 7

基本上你的查询应该有

WHERE fk_id=1234 OR fk_id is null
Run Code Online (Sandbox Code Playgroud)

所以对于sequilize它会

const Op = Sequelize.Op;

Model.findAll({
   where: {
      fk_id: {
        [Op.or]: [1234, null]
      }
   }
});
Run Code Online (Sandbox Code Playgroud)

这是文档:http : //docs.sequelizejs.com/manual/tutorial/querying.html#operators

  • 我不明白这有什么帮助。我的意思是,谁的表中 ID 为空? (3认同)