A
A
Alexander2020-02-26 19:07:33
SQL
Alexander, 2020-02-26 19:07:33

How to pull multiple values ​​in one query?

The question is probably incorrectly posed, but I don’t understand which side to approach it

in one table there are several values ​​\u200b\u200bwith category IDs,
although the entity has different fields, but the categories are in the same table
, I made such a crutch, but I understand that this is not very
I'm stupid - I don't understand how to optimize the query

$qb
            ->select('s.id')
            ->addSelect('s.title')
            ->addSelect('s.quantity')
            ->addSelect('s.productCategoryId')
            ->addSelect('cl.name as productCategoryName')
            ->addSelect('a.name as accessoryCategoryName')
            ->addSelect('s.products')
            ->addSelect('s.attributes')
            ->addSelect('s.accessoryCategoryId')
            ->addSelect('s.accessories')
            ->from(Scenario::class, 's')
            ->leftJoin(Category::class, 'cl', 'WITH', 's.productCategoryId = cl.id_category')
            ->leftJoin(Category::class, 'a', 'WITH', 's.accessoryCategoryId = a.id_category')
            ->where('s.id = :id')
            ->setParameter('id', $id);

        $query = $qb->getQuery();


here I join the table for the second time to pull out the desired value

->leftJoin(Category::class, 'cl', 'WITH', 's.productCategoryId = cl.id_category')
->leftJoin(Category::class, 'a', 'WITH', 's.accessoryCategoryId = a.id_category')

seems to be another method

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question