具有联接表条件的 JOOQ 更新表
Posted
技术标签:
【中文标题】具有联接表条件的 JOOQ 更新表【英文标题】:JOOQ Update Table with condition of a joined Table 【发布时间】:2020-02-29 21:25:29 【问题描述】:我有两个这样的表:Enrollment 和 Student
create table [LECTURE_DB].[dbo].[ENROLLMENT](
[EXAM_PASSED] bit null default ((0)),
[EXAM_TAKEN] bit null default ((0)),
[YEAR] int not null,
[LECTURE_ID] numeric(19) not null,
[STUDENT_ID] numeric(19) not null,
constraint [PK__ENROLLME__FE468F5B2739D489]
primary key (
[YEAR],
[LECTURE_ID],
[STUDENT_ID]
)
)
create table [LECTURE_DB].[dbo].[STUDENT](
[ID] numeric(19) identity(1, 1) not null,
[FIRSTNAME] varchar(255) null,
[GENDER] varchar(255) null,
[LASTNAME] varchar(255) null,
[YEAR_OF_BIRTH] int null,
constraint [PK__STUDENT__3214EC273493CFA7]
primary key ([ID])
)
我现在尝试用一些lastname
s、Lecture_ID
和year
更新一堆注册(exam_passed
列)。从this 我得到了使用连接表的想法。
private void updateStudentExamPassed(boolean passed, int lidx, int year, String... studentName)
Enrollment enrollment = Enrollment.ENROLLMENT;
Student student = Student.STUDENT;
Table<?> joined = enrollment.leftJoin(student).on(enrollment.STUDENT_ID.eq(student.ID));
ctx.update(joined)
.set(enrollment.EXAM_PASSED, passed)
.where(student.LASTNAME.in(studentName))
.and(enrollment.YEAR.eq(year))
.and(enrollment.LECTURE_ID.eq(BigInteger.valueOf(lidx)))
.execute();
但我在.execute()
上收到了DataAccessException
Exception in thread "main" org.jooq.exception.DataAccessException: SQL [update [LECTURE_DB].[dbo].[ENROLLMENT] left outer join [LECTURE_DB].[dbo].[STUDENT] on [LECTURE_DB].[dbo].[ENROLLMENT].[STUDENT_ID] = [LECTURE_DB].[dbo].[STUDENT].[ID] set [LECTURE_DB].[dbo].[ENROLLMENT].[EXAM_PASSED] = ? where ([LECTURE_DB].[dbo].[STUDENT].[LASTNAME] in (?, ?) and [LECTURE_DB].[dbo].[ENROLLMENT].[YEAR] = ? and [LECTURE_DB].[dbo].[ENROLLMENT].[LECTURE_ID] = ?)]; Falsche Syntax in der Nähe des left-Schlüsselworts.
at org.jooq_3.12.1.SQLSERVER2014.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2717)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:755)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:383)
at org.jooq.impl.AbstractDelegatingQuery.execute(AbstractDelegatingQuery.java:119)
at de.esteam.lecturedb.jooq.tasks.RecordFeaturesTask.updateStudentExamPassed(RecordFeaturesTask.java:42)
at de.esteam.lecturedb.jooq.tasks.RecordFeaturesTask.run(RecordFeaturesTask.java:28)
at de.esteam.lecturedb.jooq.common.LectureDBAnalysis.run(LectureDBAnalysis.java:100)
at de.esteam.lecturedb.jooq.common.LectureDBAnalysis.main(LectureDBAnalysis.java:56)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Falsche Syntax in der Nähe des left-Schlüsselworts.
文档对我没有帮助,因为条件依赖于另一个表。有没有希望让它发挥作用,还是我需要自己获取每个注册并单独更新它?
【问题讨论】:
【参考方案1】:使用供应商特定的语法
The question you've linked 是关于 mysql 的。 SQL Server 不支持这种语法,但您可以使用UPDATE .. FROM
子句实现等效语义。 IE。试试这个:
ctx.update(enrollment)
.set(enrollment.EXAM_PASSED, passed)
.from(joined)
.where(student.LASTNAME.in(studentName))
.and(enrollment.YEAR.eq(year))
.and(enrollment.LECTURE_ID.eq(BigInteger.valueOf(lidx)))
.execute();
请注意,如果您通过在WHERE
子句中的student
表上添加谓词将其再次转换为INNER JOIN
,则LEFT JOIN
在这里没有任何意义。
使用标准语法
我个人更喜欢在这些情况下尽可能使用标准 SQL 语法,因为这些 UPDATE .. FROM
或 UPDATE .. JOIN
语句通常可以替换为 IN
或 EXISTS
谓词。例如:
ctx.update(enrollment)
.set(enrollment.EXAM_PASSED, passed)
.where(enrollment.STUDENT_ID.in(
select(student.ID)
.from(student)
.and(student.LASTNAME.in(studentName))
))
.and(enrollment.YEAR.eq(year))
.and(enrollment.LECTURE_ID.eq(BigInteger.valueOf(lidx)))
.execute();
【讨论】:
以上是关于具有联接表条件的 JOOQ 更新表的主要内容,如果未能解决你的问题,请参考以下文章