你可以帮我在条件
中创建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条件。它的条件是随机的。然后我想创建查询。你能帮助我吗....
答案 0 :(得分:0)
我不确定“ SqlBuilder”是否是特定的库,因为有多个库具有SqlBuilder
类(例如MyBatis),并且还有其他一些库是 SQL构建器(例如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 无法处理您编写的谓词类型,也许您可以这样重写它:
WHERE (t2.creditLimit BETWEEN 70001 AND 99999) OR (t0.officeCode = 6)