fix SQL for thread lookup by target
Created by: sqs
So long as threads are 1-to-1 with targets, this luckily works because the IDs were always incremented in a lockstep fashion (although this was not guaranteed, if a crash occurred during create-thread operations, because DB transactions aren't in use).
If threads ever become not 1-to-1 with targets, the existing code would break. This code makes it not break now or if that future change is made.