Count relationship data and then do query where to database from the count

Original message was deleted