code type is sql
This commit is contained in:
parent
8c0ec95802
commit
722f7903fd
@ -4,7 +4,7 @@
|
||||
|
||||
A string literal is enclosed in single quotes; for example: 'literal'. A string literal that includes a single quote is represented by two single quotes; for example: 'literal''s'.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User WHERE User.name = 'Vincent'
|
||||
</code>
|
||||
|
||||
@ -12,7 +12,7 @@ FROM User WHERE User.name = 'Vincent'
|
||||
|
||||
Integer literals support the use of PHP integer literal syntax.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User WHERE User.id = 4
|
||||
</code>
|
||||
|
||||
@ -20,7 +20,7 @@ FROM User WHERE User.id = 4
|
||||
|
||||
Float literals support the use of PHP float literal syntax.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM Account WHERE Account.amount = 432.123
|
||||
</code>
|
||||
|
||||
@ -28,7 +28,7 @@ FROM Account WHERE Account.amount = 432.123
|
||||
|
||||
The boolean literals are true and false.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User WHERE User.admin = true
|
||||
|
||||
FROM Session WHERE Session.is_authed = false
|
||||
@ -38,7 +38,7 @@ FROM Session WHERE Session.is_authed = false
|
||||
|
||||
The enumerated values work in the same way as string literals.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User WHERE User.type = 'admin'
|
||||
</code>
|
||||
|
||||
@ -96,7 +96,7 @@ An IN conditional expression returns true if the //operand// is found from resul
|
||||
|
||||
When //value list// is being used there must be at least one element in that list.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C1 WHERE C1.col1 IN (FROM C2(col1));
|
||||
|
||||
FROM User WHERE User.id IN (1,3,4,5)
|
||||
@ -104,7 +104,7 @@ FROM User WHERE User.id IN (1,3,4,5)
|
||||
|
||||
The keyword IN is an alias for = ANY. Thus, these two statements are equal:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C1 WHERE C1.col1 = ANY (FROM C2(col1));
|
||||
FROM C1 WHERE C1.col1 IN (FROM C2(col1));
|
||||
</code>
|
||||
@ -157,7 +157,7 @@ The NOT EXISTS operator returns TRUE if the subquery returns 0 rows and FALSE ot
|
||||
|
||||
Finding all articles which have readers:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM Article a
|
||||
WHERE EXISTS (SELECT r.id FROM ReaderLog r
|
||||
WHERE r.article_id = a.id)
|
||||
@ -165,7 +165,7 @@ FROM Article a
|
||||
|
||||
Finding all articles which don't have readers:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM Article a
|
||||
WHERE NOT EXISTS (SELECT r.id FROM ReaderLog r
|
||||
WHERE r.article_id = a.id)
|
||||
@ -184,19 +184,19 @@ operand comparison_operator ALL (subquery)
|
||||
|
||||
An ALL conditional expression returns true if the comparison operation is true for all values in the result of the subquery or the result of the subquery is empty. An ALL conditional expression is false if the result of the comparison is false for at least one row, and is unknown if neither true nor false.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C WHERE C.col1 < ALL (FROM C2(col1))
|
||||
</code>
|
||||
|
||||
An ANY conditional expression returns true if the comparison operation is true for some value in the result of the subquery. An ANY conditional expression is false if the result of the subquery is empty or if the comparison operation is false for every value in the result of the subquery, and is unknown if neither true nor false.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C WHERE C.col1 > ANY (FROM C2(col1))
|
||||
</code>
|
||||
|
||||
The keyword SOME is an alias for ANY.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C WHERE C.col1 > SOME (FROM C2(col1))
|
||||
</code>
|
||||
|
||||
@ -204,7 +204,7 @@ The comparison operators that can be used with ALL or ANY conditional expression
|
||||
|
||||
NOT IN is an alias for <> ALL. Thus, these two statements are equal:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM C WHERE C.col1 <> ALL (FROM C2(col1));
|
||||
FROM C WHERE C.col1 NOT IN (FROM C2(col1));
|
||||
</code>
|
||||
|
@ -1,4 +1,4 @@
|
||||
<code>
|
||||
<code type="sql">
|
||||
DELETE FROM <component_name>
|
||||
[WHERE <where_condition>]
|
||||
[ORDER BY ...]
|
||||
|
@ -1,13 +1,13 @@
|
||||
Syntax:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM <component_reference> [[LEFT | INNER] JOIN <component_reference>] ...
|
||||
</code>
|
||||
|
||||
The {{FROM}} clause indicates the component or components from which to retrieve records. If you name more than one component, you are performing a join. For each table specified, you can optionally specify an alias.
|
||||
|
||||
Consider the following DQL query:
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User u
|
||||
</code>
|
||||
|
||||
|
@ -3,25 +3,25 @@
|
||||
|
||||
Selecting alphabetically first user by name.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT MIN(u.name) FROM User u
|
||||
</code>
|
||||
|
||||
Selecting the sum of all Account amounts.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT SUM(a.amount) FROM Account a
|
||||
</code>
|
||||
|
||||
* Using an aggregate function in a statement containing no GROUP BY clause, results in grouping on all rows. In the example above we fetch all users and the number of phonenumbers they have.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.*, COUNT(p.id) FROM User u, u.Phonenumber p GROUP BY u.id
|
||||
</code>
|
||||
|
||||
* The HAVING clause can be used for narrowing the results using aggregate values. In the following example we fetch all users which have atleast 2 phonenumbers
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.* FROM User u, u.Phonenumber p HAVING COUNT(p.id) >= 2
|
||||
</code>
|
||||
|
||||
|
@ -11,7 +11,7 @@ DQL supports two kinds of joins INNER JOINs and LEFT JOINs. For each joined comp
|
||||
|
||||
* The default join type is {{LEFT JOIN}}. This join can be indicated by the use of either {{LEFT JOIN}} clause or simply '{{,}}', hence the following queries are equal:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.*, p.* FROM User u LEFT JOIN u.Phonenumber
|
||||
|
||||
SELECT u.*, p.* FROM User u, u.Phonenumber p
|
||||
@ -21,32 +21,32 @@ The recommended form is the first one.
|
||||
|
||||
* {{INNER JOIN}} produces an intersection between two specified components (that is, each and every record in the first component is joined to each and every record in the second component). So basically {{INNER JOIN}} can be used when you want to efficiently fetch for example all users which have one or more phonenumbers.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.*, p.* FROM User u INNER JOIN u.Phonenumber p
|
||||
</code>
|
||||
|
||||
By default DQL auto-adds the primary key join condition, so for DQL query:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber
|
||||
</code>
|
||||
|
||||
Would have a SQL equivalent:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = p.user_id
|
||||
</code>
|
||||
|
||||
+++ ON keyword
|
||||
If you want to override this behaviour and add your own custom join condition you can do it with the {{ON}} keyword. Consider the following DQL query:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber ON u.id = 2
|
||||
</code>
|
||||
|
||||
This query would be converted into SQL:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = 2
|
||||
</code>
|
||||
|
||||
@ -54,12 +54,12 @@ SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id
|
||||
Most of the time you don't need to override the primary join condition, rather you may want to add some custom conditions. This can be achieved with the {{WITH}} keyword.
|
||||
|
||||
DQL:
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id, p.id FROM User u LEFT JOIN u.Phonenumber WITH u.id = 2
|
||||
</code>
|
||||
|
||||
SQL:
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id AS u__id, p.id AS p__id FROM User u LEFT JOIN Phonenumber p ON u.id = p.user_id AND u.id = 2
|
||||
</code>
|
||||
|
||||
|
@ -47,13 +47,13 @@ DQL overcomes this problem with subqueries and with complex but efficient subque
|
||||
|
||||
DQL QUERY:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id, u.name, p.* FROM User u LEFT JOIN u.Phonenumber p LIMIT 20
|
||||
</code>
|
||||
|
||||
SQL QUERY:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT
|
||||
e.id AS e__id,
|
||||
e.name AS e__name,
|
||||
@ -72,13 +72,13 @@ In the next example we are going to fetch first 20 users and all their phonenumb
|
||||
|
||||
DQL QUERY:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.id, u.name, p.* FROM User u LEFT JOIN u.Phonenumber p LIMIT 20
|
||||
</code>
|
||||
|
||||
SQL QUERY:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT
|
||||
e.id AS e__id,
|
||||
e.name AS e__name,
|
||||
|
@ -11,7 +11,7 @@ Syntax:
|
||||
|
||||
Examples:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User u LEFT JOIN u.Phonenumber p
|
||||
ORDER BY u.name, p.phonenumber
|
||||
|
||||
@ -21,7 +21,7 @@ FROM User u, u.Email e
|
||||
|
||||
In order to sort in reverse order you can add the DESC (descending) keyword to the name of the column in the ORDER BY clause that you are sorting by. The default is ascending order; this can be specified explicitly using the ASC keyword.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
FROM User u LEFT JOIN u.Email e
|
||||
ORDER BY e.address DESC, u.id ASC;
|
||||
</code>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{{SELECT}} statement syntax:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT
|
||||
[ALL | DISTINCT]
|
||||
<select_expr>, ...
|
||||
@ -18,19 +18,19 @@ The {{SELECT}} statement is used for the retrieval of data from one or more comp
|
||||
|
||||
* 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.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
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).
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT a.* FROM Account a
|
||||
</code>
|
||||
|
||||
* {{FROM}} clause {{components}} indicates the component or components from which to retrieve records.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT a.* FROM Account a
|
||||
|
||||
SELECT u.*, p.*, g.* FROM User u LEFT JOIN u.Phonenumber p LEFT JOIN u.Group g
|
||||
@ -38,26 +38,26 @@ SELECT u.*, p.*, g.* FROM User u LEFT JOIN u.Phonenumber p LEFT JOIN u.Group g
|
||||
|
||||
* 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.
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
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
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
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
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
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}}
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
SELECT u.* FROM User u LIMIT 20
|
||||
</code>
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{{UPDATE}} statement syntax:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
UPDATE //component_name//
|
||||
SET //col_name1//=//expr1// [, //col_name2//=//expr2// ...]
|
||||
[WHERE //where_condition//]
|
||||
|
@ -1,6 +1,6 @@
|
||||
Syntax:
|
||||
|
||||
<code>
|
||||
<code type="sql">
|
||||
WHERE <where_condition>
|
||||
</code>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user