Sub query in phalcon 2.0.2 do not work
$phql = "SELECT p.*, (SELECT n.creation_date FROM Uno\Models\Notifications AS n
WHERE n.post_id = p.id
AND n.user_id = ".$user_id ."
ORDER BY n.creation_date DESC
LIMIT 1
) AS cdate
FROM Uno\Models\Posts AS p
WHERE p.user_id = ".$user_id."
AND p.group_id = ".$group_id."
ORDER BY cdate DESC";
$obj_phql = new \Phalcon\Mvc\Model\Query($phql, $this->getDI());
$rs = $obj_phql->execute();
it return error even query works well:
<b>Fatal error</b>: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Syntax error, unexpected token SELECT, near to ' n.creation_date FROM Uno\Models\Notifications AS n
WHERE n.post_id = p.id
AND n.user_id = 3
ORDER BY n.creation_date DESC
LIMIT 1
) AS cdate
FROM Uno\Models\Posts AS p
WHERE p.user_id = 3
AND p.group_id = 2
ORDER BY cdate DESC', when parsing: SELECT p.*, (SELECT n.creation_date FROM Uno\Models\Notifications AS n
WHERE n.post_id = p.id
AND n.user_id = 3
ORDER BY n.creation_date DESC
LIMIT 1
) AS cdate
FROM Uno\Models\Posts AS p
WHERE p.user_id = 3
Are you using Phalcon 2.0.2?
No, I am on Phlacon 2.0.0
Phalcon 2.0.0 does not support subqueries
Thanks, let me upgrade the version.
I have upgraded 2.0.0 to 2.0.2. Subquery work well if it has been used in WHERE clause but it do not work if it has been used in fields selection like mine.
Same problem only works on where clause, cannot use subquery as column Phalcon 2.0.2
This is fixed in 2.0.3, can you try again using that version? (please do not compile from build/)
then shall I compile from ext?
2.0.3 is not the stable version, I would wait to get it stable. Meanwhile, I have executed raw sql as alternate solution.
// Base model
$bm = new BaseModel();
// Execute the query
$rs = new Phalcon\Mvc\Model\Resultset\Simple(null, $bm,$bm->getReadConnection()->query($sql));
2.0.3 is more stable than 2.0.2
May I know the steps to upgrade 2.0.2 to 2.0.3. I looked the website https://phalconphp.com/ and it has link to download 2.0.2 so I thought it is the stable release.
I had the same issue like rakesh with 2.0.2. 10 minutes ago i updated to 2.0.3 and my subquery still not working:
My Code-Part:
$sql = 'SELECT *
FROM (
SELECT *
FROM ZipGeodb
Group by zc_location_name
Order By zc_location_name) as alleStaedte
WHERE alleStaedte.zc_id NOT IN(' . $used . ')';
$orte = $this->modelsManager->executeQuery($sql);
Phalcon throw the following error:
Phalcon\Mvc\Model\Exception: Syntax error, unexpected token (, near to ' SELECT * FROM ZipGeodb Group by zc_location_name Order By zc_location_name) as alleStaedte WHERE alleStaedte.zc_id NOT IN('8390','8860','301','342','404','455','731','822','2261','3267','3720','3826','4656','5622','6304','6341','7047','8022','9651','10179','10585','11321','11924','11960','13707')',
when parsing: SELECT * FROM ( SELECT * FROM ZipGeodb Group by zc_location_name Order By zc_location_name) as alleStaedte WHERE alleStaedte.zc_id NOT IN('8390','8860','301','342','404','455','731','822','2261','3267','3720','3826','4656','5622','6304','6341','7047','8022','9651','10179','10585','11321','11924','11960','13707') (317)
@prof-dr-dipl-ing Subqueries in the from clause aren't supported, however you can use raw queries there
I'm having troubles with 2.0.3 in subqueries when I use a join on the subquery and the main query. It seems that phalcon duplicates the join, giving exceptions due to duplicate alias.
An example (not actual usage but tested)
$phql = '
SELECT * FROM App\User\Models\User user
JOIN App\User\Models\UserFriendship as friendship ON friendship.user_id = user.id
WHERE user.id NOT IN (
SELECT user.id FROM App\User\Models\User user
JOIN App\User\Models\UserFriendship as friendship ON friendship.user_id = user.id
)';
$this->modelsManager->createQuery($phql)->execute();
the warning shows the sql made:
Phalcon\Db\Adapter\Pdo->query(
SELECT `user`.`id` AS `_user_id`, `user`.`name` AS `_user_name`, `user`.`email` AS `_user_email`, `user`.`telephone` AS `_user_telephone`, `user`.`password` AS `_user_password`, `user`.`avatar` AS `_user_avatar`, `user`.`gender` AS `_user_gender`, `user`.`birth` AS `_user_birth`, `user`.`role` AS `_user_role`, `user`.`active` AS `_user_active`, `user`.`date_created` AS `_user_date_created`, `user`.`date_updated` AS `_user_date_updated`, `user`.`deleted` AS `_user_deleted`, `friendship`.`id` AS `_friendship_id`, `friendship`.`user_id` AS `_friendship_user_id`, `friendship`.`friend_id` AS `_friendship_friend_id`, `friendship`.`status` AS `_friendship_status`
FROM `user` AS `user`
INNER JOIN `user_friendship` AS `friendship` ON `friendship`.`user_id` = `user`.`id`
WHERE `user`.`id` NOT IN (
SELECT `user`.`id` AS `id`
FROM `user` AS `user`
INNER JOIN `user_friendship` AS `friendship` ON `friendship`.`user_id` = `user`.`id`
INNER JOIN `user_friendship` AS `friendship` ON `friendship`.`user_id` = `user`.`id`)
, null, null)
It does not matter the tables (in this example those are the same)
@Surt Could you please create a new issue?
#10505 here
This still not be fixed. -- Do we have any update for this issue?
https://github.com/phalcon/cphalcon/issues/10505 there is issue and it's closed beacause it's fixed. Can you share what exactly query what are you doing and what phalcon version you have ?
select * from ( select ur.jiebie_id from ulogin as ul left join user_role as ur on ur.user_id=ul.user_id where ul.user_name=:query_user: and ur.permission_id in ('a','w') ) as tt;
I don't know how to see the version. Yet , it is the latest one, maybe some sort of 2.1.*
So - those kind of selects are just not supported in phql. I think there is somwhere NFR for this.