DQL: SELECT EXISTS does not work #5419

Closed
opened 2026-01-22 15:07:16 +01:00 by admin · 3 comments
Owner

Originally created by @Erikvv on GitHub (Feb 16, 2017).

Originally assigned to: @Ocramius on GitHub.

This is based on valid SQL but I cannot get it to work in DQL. The documentation says EXISTS should work in both WHERE and SELECT.

SELECT 
    EXISTS (
        SELECT activity FROM Entity\Activity activity
        WHERE activity MEMBER OF person.activities
        AND activity.name = 'sports'
   ) AS doesSports,
   person.id 
FROM Entity\Person person

This gives the error:

[Syntax`` Error] line 0, col 7: Error: Expected known function, got 'EXISTS'

EXISTS seems to work like this after WHERE but not in select

There should be an eample in the documentation, or a fix if this is a bug.

Originally created by @Erikvv on GitHub (Feb 16, 2017). Originally assigned to: @Ocramius on GitHub. This is based on valid SQL but I cannot get it to work in DQL. The documentation says EXISTS should work in both WHERE and SELECT. ``` SELECT EXISTS ( SELECT activity FROM Entity\Activity activity WHERE activity MEMBER OF person.activities AND activity.name = 'sports' ) AS doesSports, person.id FROM Entity\Person person ``` This gives the error: `[Syntax`` Error] line 0, col 7: Error: Expected known function, got 'EXISTS'` EXISTS seems to work like this after WHERE but not in select There should be an eample in the documentation, or a fix if this is a bug.
admin added the DocumentationInvalid labels 2026-01-22 15:07:16 +01:00
admin closed this issue 2026-01-22 15:07:17 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 16, 2017):

Subqueries in the SELECT clause are not supported overall. Where did you find examples of EXISTS used in SELECT? The EBNF doesn't allow it, as far as I can see: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#query-language

@Ocramius commented on GitHub (Feb 16, 2017): Subqueries in the `SELECT` clause are not supported overall. Where did you find examples of `EXISTS` used in `SELECT`? The EBNF doesn't allow it, as far as I can see: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#query-language
Author
Owner

@Erikvv commented on GitHub (Feb 16, 2017):

Subqueries in the SELECT clause are not supported overall

D'oh that sound like something I knew but I must have forgotten.

I'm not familiar with EBNF unfortunately. It is a bit hard to use as a reference. Looks like it would be useful to spend some time studying that page.

I was confused by this bit:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#other-expressions

It notes about functions "The following functions are supported in SELECT, WHERE and HAVING clauses:"

But it notes not any restriction for "Other Expressions"

@Erikvv commented on GitHub (Feb 16, 2017): > Subqueries in the SELECT clause are not supported overall D'oh that sound like something I knew but I must have forgotten. I'm not familiar with EBNF unfortunately. It is a bit hard to use as a reference. Looks like it would be useful to spend some time studying that page. I was confused by this bit: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#other-expressions It notes about functions "The following functions are supported in SELECT, WHERE and HAVING clauses:" But it notes not any restriction for "Other Expressions"
Author
Owner

@Ocramius commented on GitHub (Feb 16, 2017):

@Erikvv the EBNF is really a simple recursive tree of allowed symbols. SQLite has similar docs, for example.

If you are able to improve the documentation on the bit you mentioned, please feel free to send a patch with edits to doctrine-query-language.rst.

Closing as invalid here, meanwhile.

@Ocramius commented on GitHub (Feb 16, 2017): @Erikvv the EBNF is really a simple recursive tree of allowed symbols. SQLite has similar docs, [for example](https://www.sqlite.org/docsrc/doc/trunk/art/syntax/all-bnf.html). If you are able to improve the documentation on the bit you mentioned, please feel free to send a patch with edits to [doctrine-query-language.rst](https://github.com/doctrine/doctrine2/blob/e4704beaf9fad5a521fe7592dec382ae209b3cc1/docs/en/reference/dql-doctrine-query-language.rst). Closing as `invalid` here, meanwhile.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5419