DELETE a FROM table_a a
LEFT JOIN table_b b
ON a.id = b.a_id
WHERE a.id IS NULL;
DSL.using(configuration()).delete(TableA).join(..)
// Does not exist: -------------------------------> ^^^
DELETE a FROM table_a a
LEFT JOIN table_b b
ON a.id = b.a_id
WHERE b.a_id IS NULL;
DSLContext deleteQuery = DSL.using(configuration());
deleteQuery
.delete(TableA)
.where(TableA.ID.in(
deleteQuery
.select(TableA.ID)
.from(TableA)
.join(TableB, JoinType.LEFT_OUTER_JOIN)
.on(TableA.ID.eq(TableB.A_ID))
.where(TableB.A_ID.isNull())
))
.execute();
TABLE_A.leftOuterJoin(TABLE_).on(...)
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
DSLContext deleteQuery = DSL.using(configuration());
deleteQuery
.delete(
TABLE_A
.leftOuterJoin(TABLE_B)
.on(TABLE_A.ID.eq(TABLE_B.A_ID))
)
.where(TABLE_B.A_ID.isNull())
.execute();
DELETE /* missing alias */ FROM table_a
LEFT JOIN table_b
ON table_a.id = table_b.a_id
WHERE table_b.a_id IS NULL;
Thanks for you reply !jOOQ actually let me write this :DSLContext deleteQuery = DSL.using(configuration());TABLE_A
deleteQuery
.delete(
.leftOuterJoin(TABLE_B)
.on(TABLE_A.ID.eq(TABLE_B.A_ID)))
.where(TABLE_B.A_ID.isNull())
.execute();
However MySQL does not validate the resulted SQL :DELETE /* missing alias */ FROM table_a
LEFT JOIN table_b
ON table_a.id = table_b.a_id
WHERE table_b.a_id IS NULL;
In my case, the TABLE_A.in() statement will be fast enough since the JOIN subquery will return just a few rows.
DELETE /* missing alias */ FROM table_a as a
LEFT JOIN table_b
ON table_a.id = table_b.a_id
WHERE table_b.a_id IS NULL;
DSLContext ctx = DSL.using(configuration);
Table<Record1<Long>> tmpTable = ctx
.select(TABLE_A.ID)
.from(
TABLE_A
.leftOuterJoin(TABLE_B)
.on(TABLE_A.ID.eq(TABLE_B.A_ID))
)
.where(TABLE_B.A_ID.isNull()).asTable("tmp");
ctx
.delete(TABLE_A)
.where(TABLE_A.ID.in(ctx.select((Field<Long>)tmpTable.field("id"))
.from(tmpTable)))
.execute();
delete from `table_a`
where `table_a`.`id` in (
select `tmp`.`id`
from (
select `table_a`.`id`
from `table_a`
left outer join `table_b`
on `table_a`.`id` = `table_b`.`a_id`
where `table_b`.`a_id` is null
) as `tmp`
)
Hi Lukas,
If I try to alias TABLE_A, I get :DELETE /* missing alias */ FROM table_a as aBut the problem is the same.
However, I just realized the TABLE_A.in() statement does not work because MySQL does not allow to delete from a table and select from the same table in a subquery: http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause :(
Alright, that will need to be fixed
Hello Lukas,
Yes it finally worked, I have tested it this time !
I think it is reliable since the solution is 6 years old (http://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause/8620162#8620162) and still nothing has changed.
However, I don't know if because of a MySQL limitation, a (big) hack should be added to jOOQ.
Maybe we can wait to see if there are other MySQL users interested in this feature before starting to think of an implementation into jOOQ.
Hi Lukas,
Thank you for this explanation, I didn't suspect this solution may corrupt the database.
However, as you pointed out in Stack Overflow, the MySQL limitation is only here because the query optimizer is lazy :)
So it might be nice if jOOQ would do the query transformation.
But so you know, for me this feature is not urgent at all.
Anyway, thank you for your time, it is nice to see that jOOQ is that much active ;)
public static Query deleteJoin(Collection<Table<?>> tables,TableOnConditionStep<Record> joinStep, Condition where){ return DSL.query("DELETE {0} FROM {1} WHERE {2}",DSL.list(tables),joinStep,where);}Query query = deleteJoin(Arrays.asList(Table.A,Table.B),Table.A.join(Table.B).on(Table.A.ID.eq(Table.B.ID)),Table.A.ID.eq(someid));
int numrows = dslContext.execute(query);--