DDC-805: Support subqueries in DQL ORDER BY #991

Closed
opened 2026-01-22 12:58:17 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 14, 2010).

Jira issue originally created by user mjh_ca:

It would be nice to be able to do something like:

// Get "most voted" polls based on number of votes to poll answers

// DQL
SELECT p, a FROM Polls p INNER JOIN p.answers a ORDER BY (SELECT SUM(a2.answers) FROM PollAnswers a2 WHERE a2.poll = p) DESC

// SQL equivalent
SELECT /** ... **/ FROM polls p INNER JOIN poll*answers a ON (p.id = a.poll_id) ORDER BY (SELECT SUM(a2.answers) FROM poll_answers a2 WHERE a2.poll*id = p.id) 

At present it seems that subqueries are not supported in ORDER BY

Originally created by @doctrinebot on GitHub (Sep 14, 2010). Jira issue originally created by user mjh_ca: It would be nice to be able to do something like: ``` // Get "most voted" polls based on number of votes to poll answers // DQL SELECT p, a FROM Polls p INNER JOIN p.answers a ORDER BY (SELECT SUM(a2.answers) FROM PollAnswers a2 WHERE a2.poll = p) DESC // SQL equivalent SELECT /** ... **/ FROM polls p INNER JOIN poll*answers a ON (p.id = a.poll_id) ORDER BY (SELECT SUM(a2.answers) FROM poll_answers a2 WHERE a2.poll*id = p.id) ``` At present it seems that subqueries are not supported in ORDER BY
admin added the Improvement label 2026-01-22 12:58:17 +01:00
admin closed this issue 2026-01-22 12:58:18 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 15, 2010):

Comment created by @beberlei:

yes, becaues not all vendors support it.

Please use your own Result Set Mappings and Native Query to query for the desired result:

http://www.doctrine-project.org/projects/orm/2.0/docs/reference/native-sql/en#native-sql

@doctrinebot commented on GitHub (Sep 15, 2010): Comment created by @beberlei: yes, becaues not all vendors support it. Please use your own Result Set Mappings and Native Query to query for the desired result: http://www.doctrine-project.org/projects/orm/2.0/docs/reference/native-sql/en#native-sql
Author
Owner

@doctrinebot commented on GitHub (Sep 15, 2010):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Sep 15, 2010): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Sep 15, 2010):

Comment created by romanb:

hm. as a side note, correlated subqueries very often perform very badly when the tables get larger.

@doctrinebot commented on GitHub (Sep 15, 2010): Comment created by romanb: hm. as a side note, correlated subqueries very often perform **very** badly when the tables get larger.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#991