DQL (Doctrine Query Language) - Conditional expressions - Exists Expressions.php 588 Bytes
Newer Older
hansbrix's avatar
hansbrix committed
1 2
Syntax:
<code>
3
<operand> [NOT ]EXISTS (<subquery>)
hansbrix's avatar
hansbrix committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
</code>
The EXISTS operator returns TRUE if the subquery returns one or more rows and FALSE otherwise. 



The NOT EXISTS operator returns TRUE if the subquery returns 0 rows and FALSE otherwise.



Finding all articles which have readers:
<code>
FROM Article
  WHERE EXISTS (FROM ReaderLog(id)
                WHERE ReaderLog.article_id = Article.id)
</code>
Finding all articles which don't have readers:
<code>
FROM Article
  WHERE NOT EXISTS (FROM ReaderLog(id)
                WHERE ReaderLog.article_id = Article.id)
</code>     
25