javascript - Typeor find with and & or where block

标签 javascript typescript typeorm

我正在尝试在 typeorm 中构建以下查询。

SELECT * from Department where
type = 'Employee' and
((from_date BETWEEN '2013-01-03'AND '2013-01-09') OR 
(to_date BETWEEN '2013-01-03' AND '2013-01-09') OR 
(from_date <= '2013-01-03' AND to_date >= '2013-01-09'))

下面是我的 typeorm 等价物。

connection.find(Department, {
      where: [
        { fromDate: Between(filter.fromDate, filter.toDate) },
        { toDate: Between(filter.fromDate, filter.toDate) },
        {
          fromDate: LessThanOrEqual(filter.fromDate),
          toDate: MoreThanOrEqual(filter.toDate),
        },
      ],
      andWhere: { type: 'Employee' },
    });

但不知何故我得到了错误的输出数量。好像 andWhere 不起作用。 提前致谢

最佳答案

let qb = this.repository.createQueryBuilder("department");

qb.where("department.type= :type", {type: "Employee"});
qb.andWhere("((department.from_date BETWEEN '2013-01-03'AND '2013-01-09') OR 
(department.to_date BETWEEN '2013-01-03' AND '2013-01-09') OR 
(department.from_date <= '2013-01-03' AND department.to_date >= '2013-01-09'))");

关于javascript - Typeor find with and & or where block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62004119/

相关文章:

angular - Angular-Material 对话框模块中的循环依赖

node.js - 如何在服务 nestjs 中模拟 getMongoRepository

node.js - TypeScript、TypeORM、@OneToMany : constructor simply does not work

typescript - NestJS 使用 .env 和 @nestjs/config 设置 TypeOrm 连接

javascript - 与 console.log 相比,dist() 返回不同的结果

javascript - 如何仅在需要时运行 Google Cloud SQL?

javascript - 通过重新定义父方法在 Jquery UI 中扩展小部件

TypeScript - 检查类是否实现接口(interface)

javascript - 我如何循环动画循环进度组件

javascript - 使用 nuxt.js 导入文件(Vue 相当于 getElementById())