select-queries.txt 2.59 KB
Newer Older
1 2
{{SELECT}} statement syntax:

jackbravo's avatar
jackbravo committed
3
<code type="sql">
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
SELECT
    [ALL | DISTINCT]
    <select_expr>, ...
    [FROM <components>
    [WHERE <where_condition>]
    [GROUP BY <groupby_expr>
      [ASC | DESC], ... ]
    [HAVING <where_condition>]
    [ORDER BY <orderby_expr>
      [ASC | DESC], ...]
    [LIMIT <row_count> OFFSET <offset>}]
</code>

The {{SELECT}} statement is used for the retrieval of data from one or more components.

* Each {{select_expr}} indicates a column or an aggregate function value that you want to retrieve. There must be at least one {{select_expr}} in every {{SELECT}} statement.

jackbravo's avatar
jackbravo committed
21
<code type="sql">
22 23 24 25 26
SELECT a.name, a.amount FROM Account a
</code>

* An asterisk can be used for selecting all columns from given component. Even when using an asterisk the executed sql queries never actually use it (Doctrine converts asterisk to appropriate column names, hence leading to better performance on some databases).

jackbravo's avatar
jackbravo committed
27
<code type="sql">
28 29 30 31 32
SELECT a.* FROM Account a
</code>

* {{FROM}} clause {{components}} indicates the component or components from which to retrieve records.

jackbravo's avatar
jackbravo committed
33
<code type="sql">
34 35 36 37 38 39 40
SELECT a.* FROM Account a

SELECT u.*, p.*, g.* FROM User u LEFT JOIN u.Phonenumber p LEFT JOIN u.Group g
</code>

* The {{WHERE}} clause, if given, indicates the condition or conditions that the records must satisfy to be selected. {{where_condition}} is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no {{WHERE}} clause.

jackbravo's avatar
jackbravo committed
41
<code type="sql">
42 43 44 45 46 47
SELECT a.* FROM Account a WHERE a.amount > 2000
</code>

* In the {{WHERE}} clause, you can use any of the functions and operators that DQL supports, except for aggregate (summary) functions
* The {{HAVING}} clause can be used for narrowing the results with aggregate functions

jackbravo's avatar
jackbravo committed
48
<code type="sql">
49 50 51 52 53
SELECT u.* FROM User u LEFT JOIN u.Phonenumber p HAVING COUNT(p.id) > 3
</code>

* The {{ORDER BY}} clause can be used for sorting the results

jackbravo's avatar
jackbravo committed
54
<code type="sql">
55 56 57 58 59
SELECT u.* FROM User u ORDER BY u.name
</code>

* The {{LIMIT}} and {{OFFSET}} clauses can be used for efficiently limiting the number of records to a given {{row_count}}

jackbravo's avatar
jackbravo committed
60
<code type="sql">
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
SELECT u.* FROM User u LIMIT 20
</code>


+++ DISTINCT keyword


+++ Aggregate values

Aggregate value {{SELECT}} syntax:

<code type="php">
// SELECT u.*, COUNT(p.id) num_posts FROM User u, u.Posts p WHERE u.id = 1 GROUP BY u.id

$query = new Doctrine_Query();

$query->select('u.*, COUNT(p.id) num_posts')
      ->from('User u, u.Posts p')
      ->where('u.id = ?', 1)
      ->groupby('u.id');

$users = $query->execute();

echo $users->Posts[0]->num_posts . ' posts found';
</code>