你能帮我在where条件下创建sql查询吗
SELECT * FROM classicmodels.offices t1 INNER JOIN classicmodels.employees t0
ON (t1.officeCode = t0.officeCode) INNER JOIN classicmodels.customers t2 ON
( (t0.employeeNumber = t2.salesRepEmployeeNumber) OR (t0.firstName =
t2.contactFirstName) ) WHERE (t2.creditLimit > 70000) AND (t2.creditLimit <
100000) OR (t0.officeCode = 6)
ORDER BY
t0.firstName ASC
我无法创建请告诉我如何在单个查询中创建 AND 或 OR 两个条件
WHERE (t2.creditLimit > 70000) AND (t2.creditLimit < 100000) OR (t0.officeCode = 6)
如何创建动态查询,可以是多次 AND 条件,也可以是多次 OR 条件。它的条件是随机的。然后我想创建查询。你能帮我吗....
最佳答案
我不确定“SqlBuilder”是否是一个特定的库,因为有几个库具有 SqlBuilder
类(例如 MyBatis ),并且还有其他库是 SQL 构建器(例如 jOOQ )。所以,我只是为 jOOQ 回答这个问题(我为供应商工作)。
使用jOOQ
假设您使用代码生成器,您的查询可以这样编写:
// Aliasing is not necessary in your case, but here it is, still:
Offices t1 = OFFICES.as("t1");
Employees t0 = EMPLOYEES.as("t0");
Customers t2 = CUSTOMERS.as("t2");
Result<?> result =
ctx.select()
.from(t1)
.join(t0).on(t1.OFFICECODE.eq(t0.OFFICECODE))
.join(t2).on(t0.EMPLOYEENUMBER.eq(t2.SALESREPEMPLOYEENUMBER)
.or(t0.FIRSTNAME.eq(t2.CONTACTFIRSTNAME)))
.where(t2.CREDITLIMIT.gt(70000))
.and(t2.CREDITLIMIT.lt(100000))
.or(t0.OFFICECODE.eq(6))
.orderBy(t0.FIRSTNAME)
.fetch();
使用其他一些特定的 SqlBuilder
如果您的特定SqlBuilder无法处理您编写的谓词类型,也许您可以将其重写如下:
WHERE (t2.creditLimit BETWEEN 70001 AND 99999) OR (t0.officeCode = 6)
关于java - 在java中使用SqlBuilder创建动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461344/