如何使用规范在 mysql 查询中使两个 "OR" 条件具有多个 "AND" 条件
How to use specification to make two "OR" condition with multiple "AND" condition in mysql query
我想在规范的帮助下进行以下查询
select * from table where (order_quantity > 0 or product_verified = false) and sku = "12345";
以下是我的代码,但我得到了错误的结果集
Specification<JitOrderItem> specification = JitOrderItemSpecification.findAll();
specification = Specifications.where(specification).and(
Specifications.where(specification).or(JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0))
.or(Specifications.where(specification).and(JitOrderItemSpecification.filterByProductVerified(false)))
);
specification = Specifications.where(specification).and(JitOrderItemSpecification.filterBySku(sku));
我假设您正在使用 JPA,可能是休眠。您可以启用日志记录(对于休眠,它是 "org.hibernate.sql" 进行调试)。这样您就可以看到生成的查询,这可能有助于您自己调试这些查询。
查看您的代码,您的最终规范如下所示:
Specifications.where(
Specifications.where(JitOrderItemSpecification.findAll())
.and(
Specifications.where(JitOrderItemSpecification.findAll())
.or(JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0))
.or(Specifications.where(JitOrderItemSpecification.findAll()).and(JitOrderItemSpecification.filterByProductVerified(false)))
)
)
.and(JitOrderItemSpecification.filterBySku(sku));
您基本上是在查询以下内容(仅限 WHERE 子句):
(
ALL JitOrderItems
AND
(
ALL JitOrderItems
OR
OrderQuantityGreaterThan 0
OR
(
ALL JitOrderItems
AND
ProductVerified false
)
)
)
AND
JitOrderItemSpec.filterBySku(sku)
你想要的大概是:
Specifications.where(
JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0).or(JitOrderItemSpecification.filterByProductVerified(false)))
.and(JitOrderItemSpecification.filterBySku(sku));
我想在规范的帮助下进行以下查询
select * from table where (order_quantity > 0 or product_verified = false) and sku = "12345";
以下是我的代码,但我得到了错误的结果集
Specification<JitOrderItem> specification = JitOrderItemSpecification.findAll();
specification = Specifications.where(specification).and(
Specifications.where(specification).or(JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0))
.or(Specifications.where(specification).and(JitOrderItemSpecification.filterByProductVerified(false)))
);
specification = Specifications.where(specification).and(JitOrderItemSpecification.filterBySku(sku));
我假设您正在使用 JPA,可能是休眠。您可以启用日志记录(对于休眠,它是 "org.hibernate.sql" 进行调试)。这样您就可以看到生成的查询,这可能有助于您自己调试这些查询。
查看您的代码,您的最终规范如下所示:
Specifications.where(
Specifications.where(JitOrderItemSpecification.findAll())
.and(
Specifications.where(JitOrderItemSpecification.findAll())
.or(JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0))
.or(Specifications.where(JitOrderItemSpecification.findAll()).and(JitOrderItemSpecification.filterByProductVerified(false)))
)
)
.and(JitOrderItemSpecification.filterBySku(sku));
您基本上是在查询以下内容(仅限 WHERE 子句):
(
ALL JitOrderItems
AND
(
ALL JitOrderItems
OR
OrderQuantityGreaterThan 0
OR
(
ALL JitOrderItems
AND
ProductVerified false
)
)
)
AND
JitOrderItemSpec.filterBySku(sku)
你想要的大概是:
Specifications.where(
JitOrderItemSpecification.filterByJitOrderQuantityGreaterThan(0).or(JitOrderItemSpecification.filterByProductVerified(false)))
.and(JitOrderItemSpecification.filterBySku(sku));