Using Innerjoin with Limit

Asked

Viewed 57 times

0

I have the table A with a relationship OneToMany with the table B.

The table A is small, has 10 records.

The table B has N records for each record in the table A.

From X to X minutes a script is executed that exports the 50 new records from the table B (for each record in the table A) to a file txt, for example: the table A has 10 records, so the system looks in the table B the 50 new records for each record in the table A, then it will be 10x50, then it will be exported to the file txt 500 records.

For that I made a innerjoin in the repository:

class AppsRepository extends EntityRepository
{

    public function findNewClients()
    {
        $qb = $this->createQueryBuilder('a');
        $qb
            ->innerJoin('AppBundle\Entity\Clients', 'b', 'WITH', 'b.appId = a.id')
            ->where('b.proccessed is null')
    ;

        return $qb->getQuery()->getResult();
    }
}

I need to limit the innerjoin to return only 50 records for each record in the table A as I explained above.

2 answers

0


Do so:

class AppsRepository extends EntityRepository
{
    public function findNewClients()
    {
        return $this
            ->getEntityManager()
            ->createQuery('
                SELECT AppBundle:Apps a
                JOIN a.clients c
                WHERE c.processed IS NULL')
            ->setMaxResults(50)
            ->getResult();
    }
}

-1

And if you use a LEFT JOIN or RIGHT JOIN?

Something like this:

SELECT t1.coluna1, t2.coluna1 FROM Tabela1 AS t1 LEFT JOIN table2 AS t2 ON t1.coluna1 = t2.coluna1 LIMIT 50

Browser other questions tagged

You are not signed in. Login or sign up in order to post.