Java JOOQ使用联接表的条件更新表
我有两张这样的桌子:入学和学生Java JOOQ使用联接表的条件更新表,java,sql-server,sql-update,jooq,Java,Sql Server,Sql Update,Jooq,我有两张这样的桌子:入学和学生 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__ENROLL
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])
)
我现在试着用一些姓氏、一个讲座ID和一年来更新一堆报名表——考试通过栏。从那时起,我就有了使用联接表的想法
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();
}
文档对我没有帮助,因为条件依赖于另一个表。是否有任何希望让它发挥作用,或者我是否需要自己获取每个注册并单独更新它 使用特定于供应商的语法
是关于MySQL的。SQLServer不支持这种语法,但可以通过使用子句实现等效的语义。也就是说,试试这个:
ctx.updaterollment
.setenrollment.EXAM_通过了,通过了
.fromjoin
.where student.LASTNAME.instudentName
.和/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或
.andenrollment.document\u ID.eqbiginger.valueOflidx
处决
请注意,如果在WHERE子句中的学生表上有一个谓词,将左连接再次转换为内部连接,则左连接在这里没有任何意义
使用标准语法
我个人更喜欢在这些情况下尽可能使用标准的SQL语法,因为这些更新。。来自或更新。。JOIN语句通常可以替换为IN或EXISTS谓词。例如:
ctx.updaterollment
.setenrollment.EXAM_通过了,通过了
.where registration.STUDENT\u ID.in
选择student.ID
fromstudent先生
.和student.LASTNAME.instudentName
.和/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或
.andenrollment.document\u ID.eqbiginger.valueOflidx
处决
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.