You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Sequelize实现含OR条件的Transactions表查询?

用Sequelize实现指定查询的方法

要实现等价于SQL语句 SELECT * FROM ng_db.public."Transactions" WHERE credited_account = 1 OR debited_account = 1; 的查询,你可以借助Sequelize的Op.or操作符构造OR条件,具体步骤如下:

  • 确认模型关联正确
    先确保你的Transactions模型已正确关联到目标表:

    const Transactions = sequelize.define('Transactions', {
      // 在此定义表字段,比如credited_account、debited_account等
    }, {
      tableName: 'Transactions',
      schema: 'public',
      database: 'ng_db'
    });
    
  • 执行查询
    导入Sequelize的操作符Op,通过findAll方法获取符合条件的所有记录:

    const { Op } = require('sequelize');
    
    const targetTransactions = await Transactions.findAll({
      where: {
        [Op.or]: [
          { credited_account: 1 },
          { debited_account: 1 }
        ]
      }
    });
    

这段代码会生成和你提供的SQL完全等价的查询逻辑,返回所有credited_account为1或debited_account为1的记录。

内容的提问来源于stack exchange,提问作者Pedro Vieira Lopes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 04:45:29