https://kotlinlang.org logo
#exposed
Title
# exposed
r

runjorel

07/07/2019, 6:15 AM
How can I use a sub query in an update statement ? Contrived Example:
Copy code
UPDATE Users SET active=false WHERE id IN (SELECT user_id FROM deactivated)
It seems the only option I have is
inList
but I am afraid if I call
map{..}/toList()
on my subquery it would materialize those results.
t

tapac

07/08/2019, 7:16 PM
It’s better to update it like this:
Copy code
Users.innerjoin(Deactivated).update {
    it[Users.active] = false 
}
But if you want to use subquery than:
Copy code
val subquery = wrapAsExpression<List<EntityId<Int>>(Deactivated.slice(Deactivated.userId).selectAll())
Users.update( where = {
Users.id inList subquery}) {
   it[Users.active] = false 
}
r

runjorel

07/08/2019, 9:13 PM
thank you!
119 Views