We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

phalcon subquery

$query = $this->modelsManager->createQuery("SELECT * FROM product p WHERE cid > 1 AND (SELECT COUNT(*) FROM product p1 WHERE p1.cid = p.cid AND p.id <= p1.id) <= 4 ORDER BY p.id DESC");
return $query->execute();
Syntax error, unexpected token SELECT, near to ' COUNT(*) FROM product p1 WHERE p1.cid = p.cid AND p.id <= p1.id) <= 4 ORDER BY p.id DESC', when parsing: SELECT * FROM product p WHERE cid > 1 AND (SELECT COUNT(*) FROM product p1 WHERE p1.cid = p.cid AND p.id <= p1.id) <= 4 ORDER BY p.id DESC (138)

I want run subquery ........ help me.

SELECT * FROM product p WHERE cid > 1 AND (SELECT COUNT(*) FROM product p1 WHERE p1.cid = p.cid AND p.id <= p1.id) <= 4 ORDER BY p.id DESC


98.9k

Phalcon does not support subqueries right now. You can use left/right/inner joins instead.

inb4: it would be great that Phalcon support subqueries

Rinalds Uzkalns did find a workaround:

https://forum.phalcon.io/discussion/1082/sneaking-in-subqueries-into-phql#C4020

With 2.0 it could be easier to add subqueries, the thing is the parser is still in C..