Answer the question
In order to leave comments, you need to log in
How to combine parameters in native query?
Colleagues, the most stupid question!
Share the practice of combining query parameters.
Regarding nativeness, I'm trying to make the most optimal query.
What do I want to receive?
Number of companies, according to the specified
SQL parameters
This query, surprisingly, calculates as quickly as possible.
EXPLAIN
SELECT COUNT(o.id)
FROM organizations o
LEFT JOIN organizations_categories oc ON o.id = oc.organization_id AND oc.category_id = 9
WHERE o.id IN (
SELECT f.organization_id
FROM features f
WHERE f.name = (
SELECT f2.name
FROM features f2
WHERE f2.id = 5685427
)
)
AND o.city_id = 5128 AND oc.category_id = 9
/**
* @param string $q
* @param int $city_id
* @param int $category_id
* @param int $feature_id
* @return void
* @throws NonUniqueResultException
*/
public function getCount($q = "", int $city_id = 0, int $category_id = 0, int $feature_id = 0)
{
$rsm = new ResultSetMapping;
$rsm->addScalarResult('COUNT(o.id)', 'total');
$query = $this->getEntityManager()->createNativeQuery("
SELECT COUNT(o.id)
FROM organizations o
LEFT JOIN organizations_categories oc ON o.id = oc.organization_id AND oc.category_id = 9
WHERE o.id IN (
SELECT f.organization_id
FROM features f
WHERE f.name = (
SELECT f2.name
FROM features f2
WHERE f2.id = 5685427
)
)
AND o.city_id = 5128 AND oc.category_id = 9
", $rsm);
return $query->getSingleScalarResult();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question