1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.db.select">
4 <title>Zend_Db_Select</title>
6 <sect2 id="zend.db.select.introduction">
7 <title>Introduction</title>
10 The <classname>Zend_Db_Select</classname> object represents a <acronym>SQL</acronym>
11 <acronym>SELECT</acronym> query statement. The class has methods for adding individual
12 parts to the query. You can specify some parts of the query using <acronym>PHP</acronym>
13 methods and data structures, and the class forms the correct <acronym>SQL</acronym>
14 syntax for you. After you build a query, you can execute the query as if you had written
19 The value offered by <classname>Zend_Db_Select</classname> includes:
25 Object-oriented methods for specifying <acronym>SQL</acronym> queries in a
26 piece-by-piece manner;
32 Database-independent abstraction of some parts of the <acronym>SQL</acronym>
39 Automatic quoting of metadata identifiers in most cases, to support identifiers
40 containing <acronym>SQL</acronym> reserved words and special characters;
46 Quoting identifiers and values, to help reduce risk of <acronym>SQL</acronym>
53 Using <classname>Zend_Db_Select</classname> is not mandatory. For very simple
54 <acronym>SELECT</acronym> queries, it is usually simpler to specify the entire
55 <acronym>SQL</acronym> query as a string and execute it using Adapter methods like
56 <methodname>query()</methodname> or <methodname>fetchAll()</methodname>. Using
57 <classname>Zend_Db_Select</classname> is helpful if you need to assemble a
58 <acronym>SELECT</acronym> query procedurally, or based on conditional logic in your
63 <sect2 id="zend.db.select.creating">
64 <title>Creating a Select Object</title>
67 You can create an instance of a <classname>Zend_Db_Select</classname> object using the
68 <methodname>select()</methodname> method of a
69 <classname>Zend_Db_Adapter_Abstract</classname> object.
72 <example id="zend.db.select.creating.example-db">
73 <title>Example of the database adapter's select() method</title>
75 <programlisting language="php"><![CDATA[
76 $db = Zend_Db::factory( ...options... );
77 $select = $db->select();
82 Another way to create a <classname>Zend_Db_Select</classname> object is with its
83 constructor, specifying the database adapter as an argument.
86 <example id="zend.db.select.creating.example-new">
87 <title>Example of creating a new Select object</title>
89 <programlisting language="php"><![CDATA[
90 $db = Zend_Db::factory( ...options... );
91 $select = new Zend_Db_Select($db);
96 <sect2 id="zend.db.select.building">
97 <title>Building Select queries</title>
100 When building the query, you can add clauses of the query one by one. There is a
101 separate method to add each clause to the <classname>Zend_Db_Select</classname> object.
104 <example id="zend.db.select.building.example">
105 <title>Example of the using methods to add clauses</title>
107 <programlisting language="php"><![CDATA[
108 // Create the Zend_Db_Select object
109 $select = $db->select();
112 $select->from( ...specify table and columns... )
114 // Add a WHERE clause
115 $select->where( ...specify search criteria... )
117 // Add an ORDER BY clause
118 $select->order( ...specify sorting criteria... );
123 You also can use most methods of the <classname>Zend_Db_Select</classname> object with a
124 convenient fluent interface. A fluent interface means that each method returns a
125 reference to the object on which it was called, so you can immediately call another
129 <example id="zend.db.select.building.example-fluent">
130 <title>Example of the using the fluent interface</title>
132 <programlisting language="php"><![CDATA[
133 $select = $db->select()
134 ->from( ...specify table and columns... )
135 ->where( ...specify search criteria... )
136 ->order( ...specify sorting criteria... );
141 The examples in this section show usage of the fluent interface, but you can use the
142 non-fluent interface in all cases. It is often necessary to use the non-fluent
143 interface, for example, if your application needs to perform some logic before adding a
147 <sect3 id="zend.db.select.building.from">
148 <title>Adding a FROM clause</title>
151 Specify the table for this query using the <methodname>from()</methodname> method.
152 You can specify the table name as a simple string.
153 <classname>Zend_Db_Select</classname> applies identifier quoting around the table
154 name, so you can use special characters.
157 <example id="zend.db.select.building.from.example">
158 <title>Example of the from() method</title>
160 <programlisting language="php"><![CDATA[
165 $select = $db->select()
166 ->from( 'products' );
171 You can also specify the correlation name (sometimes called the "table alias") for
172 a table. Instead of a simple string, use an associative array mapping the
173 correlation name to the table name. In other clauses of the <acronym>SQL</acronym>
174 query, use this correlation name. If your query joins more than one table,
175 <classname>Zend_Db_Select</classname> generates unique correlation names based on
176 the table names, for any tables for which you don't specify the correlation name.
179 <example id="zend.db.select.building.from.example-cname">
180 <title>Example of specifying a table correlation name</title>
182 <programlisting language="php"><![CDATA[
185 // FROM "products" AS p
187 $select = $db->select()
188 ->from( array('p' => 'products') );
193 Some <acronym>RDBMS</acronym> brands support a leading schema specifier for a table.
194 You can specify the table name as "<command>schemaName.tableName</command>", where
195 <classname>Zend_Db_Select</classname> quotes each part individually, or you may
196 specify the schema name separately. A schema name specified in the table name takes
197 precedence over a schema provided separately in the event that both are provided.
200 <example id="zend.db.select.building.from.example-schema">
201 <title>Example of specifying a schema name</title>
203 <programlisting language="php"><![CDATA[
206 // FROM "myschema"."products"
208 $select = $db->select()
209 ->from( 'myschema.products' );
213 $select = $db->select()
214 ->from('products', '*', 'myschema');
219 <sect3 id="zend.db.select.building.columns">
220 <title>Adding Columns</title>
223 In the second argument of the <methodname>from()</methodname> method, you can
224 specify the columns to select from the respective table. If you specify no columns,
225 the default is "<emphasis>*</emphasis>", the <acronym>SQL</acronym> wildcard for
230 You can list the columns in a simple array of strings, or as an associative mapping
231 of column alias to column name. If you only have one column to query, and you don't
232 need to specify a column alias, you can list it as a plain string instead of an
237 If you give an empty array as the columns argument, no columns from the respective
238 table are included in the result set. See a
239 <link linkend="zend.db.select.building.join.example-no-columns">code example</link>
240 under the section on the <methodname>join()</methodname> method.
244 You can specify the column name as "<command>correlationName.columnName</command>".
245 <classname>Zend_Db_Select</classname> quotes each part individually. If you don't
246 specify a correlation name for a column, it uses the correlation name for the table
247 named in the current <methodname>from()</methodname> method.
250 <example id="zend.db.select.building.columns.example">
251 <title>Examples of specifying columns</title>
253 <programlisting language="php"><![CDATA[
255 // SELECT p."product_id", p."product_name"
256 // FROM "products" AS p
258 $select = $db->select()
259 ->from(array('p' => 'products'),
260 array('product_id', 'product_name'));
262 // Build the same query, specifying correlation names:
263 // SELECT p."product_id", p."product_name"
264 // FROM "products" AS p
266 $select = $db->select()
267 ->from(array('p' => 'products'),
268 array('p.product_id', 'p.product_name'));
270 // Build this query with an alias for one column:
271 // SELECT p."product_id" AS prodno, p."product_name"
272 // FROM "products" AS p
274 $select = $db->select()
275 ->from(array('p' => 'products'),
276 array('prodno' => 'product_id', 'product_name'));
281 <sect3 id="zend.db.select.building.columns-expr">
282 <title>Adding Expression Columns</title>
285 Columns in <acronym>SQL</acronym> queries are sometimes expressions, not simply
286 column names from a table. Expressions should not have correlation names or quoting
287 applied. If your column string contains parentheses,
288 <classname>Zend_Db_Select</classname> recognizes it as an expression.
292 You also can create an object of type <classname>Zend_Db_Expr</classname>
293 explicitly, to prevent a string from being treated as a column name.
294 <classname>Zend_Db_Expr</classname> is a minimal class that contains a single
295 string. <classname>Zend_Db_Select</classname> recognizes objects of type
296 <classname>Zend_Db_Expr</classname> and converts them back to string, but does not
297 apply any alterations, such as quoting or correlation names.
302 Using <classname>Zend_Db_Expr</classname> for column names is not necessary if
303 your column expression contains parentheses;
304 <classname>Zend_Db_Select</classname> recognizes parentheses and treats the
305 string as an expression, skipping quoting and correlation names.
309 <example id="zend.db.select.building.columns-expr.example">
310 <title>Examples of specifying columns containing expressions</title>
312 <programlisting language="php"><![CDATA[
314 // SELECT p."product_id", LOWER(product_name)
315 // FROM "products" AS p
316 // An expression with parentheses implicitly becomes
319 $select = $db->select()
320 ->from(array('p' => 'products'),
321 array('product_id', 'LOWER(product_name)'));
324 // SELECT p."product_id", (p.cost * 1.08) AS cost_plus_tax
325 // FROM "products" AS p
327 $select = $db->select()
328 ->from(array('p' => 'products'),
330 'cost_plus_tax' => '(p.cost * 1.08)')
333 // Build this query using Zend_Db_Expr explicitly:
334 // SELECT p."product_id", p.cost * 1.08 AS cost_plus_tax
335 // FROM "products" AS p
337 $select = $db->select()
338 ->from(array('p' => 'products'),
341 new Zend_Db_Expr('p.cost * 1.08'))
347 In the cases above, <classname>Zend_Db_Select</classname> does not alter the string
348 to apply correlation names or identifier quoting. If those changes are necessary to
349 resolve ambiguity, you must make the changes manually in the string.
353 If your column names are <acronym>SQL</acronym> keywords or contain special
354 characters, you should use the Adapter's <methodname>quoteIdentifier()</methodname>
355 method and interpolate the result into the string. The
356 <methodname>quoteIdentifier()</methodname> method uses <acronym>SQL</acronym>
357 quoting to delimit the identifier, which makes it clear that it is an identifier for
358 a table or a column, and not any other part of <acronym>SQL</acronym> syntax.
362 Your code is more database-independent if you use the
363 <methodname>quoteIdentifier()</methodname> method instead of typing quotes literally
364 in your string, because some <acronym>RDBMS</acronym> brands use nonstandard symbols
365 for quoting identifiers. The <methodname>quoteIdentifier()</methodname> method is
366 designed to use the appropriate quoting symbols based on the adapter type. The
367 <methodname>quoteIdentifier()</methodname> method also escapes any quote characters
368 that appear within the identifier name itself.
371 <example id="zend.db.select.building.columns-quoteid.example">
372 <title>Examples of quoting columns in an expression</title>
374 <programlisting language="php"><![CDATA[
376 // quoting the special column name "from" in the expression:
377 // SELECT p."from" + 10 AS origin
378 // FROM "products" AS p
380 $select = $db->select()
381 ->from(array('p' => 'products'),
383 '(p.' . $db->quoteIdentifier('from') . ' + 10)')
389 <sect3 id="zend.db.select.building.columns-atomic">
390 <title>Adding columns to an existing FROM or JOIN table</title>
393 There may be cases where you wish to add columns to an existing
394 <acronym>FROM</acronym> or <acronym>JOIN</acronym> table after those methods have
395 been called. The <methodname>columns()</methodname> method allows you to add
396 specific columns at any point before the query is executed. You can supply the
397 columns as either a string or <classname>Zend_Db_Expr</classname> or as an array of
398 these elements. The second argument to this method can be omitted, implying that the
399 columns are to be added to the <acronym>FROM</acronym> table, otherwise an existing
400 correlation name must be used.
403 <example id="zend.db.select.building.columns-atomic.example">
404 <title>Examples of adding columns with the columns() method</title>
406 <programlisting language="php"><![CDATA[
408 // SELECT p."product_id", p."product_name"
409 // FROM "products" AS p
411 $select = $db->select()
412 ->from(array('p' => 'products'), 'product_id')
413 ->columns('product_name');
415 // Build the same query, specifying correlation names:
416 // SELECT p."product_id", p."product_name"
417 // FROM "products" AS p
419 $select = $db->select()
420 ->from(array('p' => 'products'), 'p.product_id')
421 ->columns('product_name', 'p');
422 // Alternatively use columns('p.product_name')
427 <sect3 id="zend.db.select.building.join">
428 <title>Adding Another Table to the Query with JOIN</title>
431 Many useful queries involve using a <acronym>JOIN</acronym> to combine rows from
432 multiple tables. You can add tables to a <classname>Zend_Db_Select</classname> query
433 using the <methodname>join()</methodname> method. Using this method is similar to
434 the <methodname>from()</methodname> method, except you can also specify a join
435 condition in most cases.
438 <example id="zend.db.select.building.join.example">
439 <title>Example of the join() method</title>
441 <programlisting language="php"><![CDATA[
443 // SELECT p."product_id", p."product_name", l.*
444 // FROM "products" AS p JOIN "line_items" AS l
445 // ON p.product_id = l.product_id
447 $select = $db->select()
448 ->from(array('p' => 'products'),
449 array('product_id', 'product_name'))
450 ->join(array('l' => 'line_items'),
451 'p.product_id = l.product_id');
456 The second argument to <methodname>join()</methodname> is a string that is the join
457 condition. This is an expression that declares the criteria by which rows in one
458 table match rows in the other table. You can use correlation names in this
464 No quoting is applied to the expression you specify for the join condition; if
465 you have column names that need to be quoted, you must use
466 <methodname>quoteIdentifier()</methodname> as you form the string for the join
472 The third argument to <methodname>join()</methodname> is an array of column names,
473 like that used in the <methodname>from()</methodname> method. It defaults to
474 "<emphasis>*</emphasis>", supports correlation names, expressions, and
475 <classname>Zend_Db_Expr</classname> in the same way as the array of column names in
476 the <methodname>from()</methodname> method.
480 To select no columns from a table, use an empty array for the list of columns. This
481 usage works in the <methodname>from()</methodname> method too, but typically you
482 want some columns from the primary table in your queries, whereas you might want no
483 columns from a joined table.
486 <example id="zend.db.select.building.join.example-no-columns">
487 <title>Example of specifying no columns</title>
489 <programlisting language="php"><![CDATA[
491 // SELECT p."product_id", p."product_name"
492 // FROM "products" AS p JOIN "line_items" AS l
493 // ON p.product_id = l.product_id
495 $select = $db->select()
496 ->from(array('p' => 'products'),
497 array('product_id', 'product_name'))
498 ->join(array('l' => 'line_items'),
499 'p.product_id = l.product_id',
500 array() ); // empty list of columns
504 Note the empty <methodname>array()</methodname> in the above example in place of
505 a list of columns from the joined table.
510 <acronym>SQL</acronym> has several types of joins. See the list below for the
511 methods to support different join types in <classname>Zend_Db_Select</classname>.
517 <command>INNER JOIN</command> with the
518 <methodname>join(table, join, [columns])</methodname> or
519 <methodname>joinInner(table, join, [columns])</methodname> methods.
523 This may be the most common type of join. Rows from each table are compared
524 using the join condition you specify. The result set includes only the rows
525 that satisfy the join condition. The result set can be empty if no rows
526 satisfy this condition.
530 All <acronym>RDBMS</acronym> brands support this join type.
536 <command>LEFT JOIN</command> with the
537 <methodname>joinLeft(table, condition, [columns])</methodname> method.
541 All rows from the left operand table are included, matching rows from the
542 right operand table included, and the columns from the right operand table
543 are filled with <constant>NULL</constant> if no row exists matching the left
548 All <acronym>RDBMS</acronym> brands support this join type.
554 <command>RIGHT JOIN</command> with the
555 <methodname>joinRight(table, condition, [columns])</methodname> method.
559 Right outer join is the complement of left outer join. All rows from the
560 right operand table are included, matching rows from the left operand table
561 included, and the columns from the left operand table are filled with
562 <constant>NULL</constant>'s if no row exists matching the right table.
566 Some <acronym>RDBMS</acronym> brands don't support this join type, but in
567 general any right join can be represented as a left join by reversing the
574 <command>FULL JOIN</command> with the
575 <methodname>joinFull(table, condition, [columns])</methodname> method.
579 A full outer join is like combining a left outer join and a right outer
580 join. All rows from both tables are included, paired with each other on the
581 same row of the result set if they satisfy the join condition, and
582 otherwise paired with <constant>NULL</constant>'s in place of columns from
587 Some <acronym>RDBMS</acronym> brands don't support this join type.
593 <command>CROSS JOIN</command> with the
594 <methodname>joinCross(table, [columns])</methodname> method.
598 A cross join is a Cartesian product. Every row in the first table is
599 matched to every row in the second table. Therefore the number of rows in
600 the result set is equal to the product of the number of rows in each table.
601 You can filter the result set using conditions in a <acronym>WHERE</acronym>
602 clause; in this way a cross join is similar to the old
603 <acronym>SQL</acronym>-89 join syntax.
607 The <methodname>joinCross()</methodname> method has no parameter to specify
608 the join condition. Some <acronym>RDBMS</acronym> brands don't support this
615 <command>NATURAL JOIN</command> with the
616 <methodname>joinNatural(table, [columns])</methodname> method.
620 A natural join compares any columns that appear with the same name in
621 both tables. The comparison is equality of all the columns; comparing the
622 columns using inequality is not a natural join. Only natural inner joins
623 are supported by this <acronym>API</acronym>, even though
624 <acronym>SQL</acronym> permits natural outer joins as well.
628 The <methodname>joinNatural()</methodname> method has no parameter to
629 specify the join condition.
635 In addition to these join methods, you can simplify your queries by using the
636 JoinUsing methods. Instead of supplying a full condition to your join, you simply
637 pass the column name on which to join and the <classname>Zend_Db_Select</classname>
638 object completes the condition for you.
641 <example id="zend.db.select.building.joinusing.example">
642 <title>Example of the joinUsing() method</title>
644 <programlisting language="php"><![CDATA[
649 // ON "table1".column1 = "table2".column1
650 // WHERE column2 = 'foo'
652 $select = $db->select()
654 ->joinUsing('table2', 'column1')
655 ->where('column2 = ?', 'foo');
660 Each of the applicable join methods in the <classname>Zend_Db_Select</classname>
661 component has a corresponding 'using' method.
667 <methodname>joinUsing(table, join, [columns])</methodname> and
668 <methodname>joinInnerUsing(table, join, [columns])</methodname>
674 <methodname>joinLeftUsing(table, join, [columns])</methodname>
680 <methodname>joinRightUsing(table, join, [columns])</methodname>
686 <methodname>joinFullUsing(table, join, [columns])</methodname>
692 <sect3 id="zend.db.select.building.where">
693 <title>Adding a WHERE Clause</title>
696 You can specify criteria for restricting rows of the result set using the
697 <methodname>where()</methodname> method. The first argument of this method is a
698 <acronym>SQL</acronym> expression, and this expression is used in a
699 <acronym>SQL</acronym> <acronym>WHERE</acronym> clause in the query.
702 <example id="zend.db.select.building.where.example">
703 <title>Example of the where() method</title>
705 <programlisting language="php"><![CDATA[
707 // SELECT product_id, product_name, price
709 // WHERE price > 100.00
711 $select = $db->select()
713 array('product_id', 'product_name', 'price'))
714 ->where('price > 100.00');
720 No quoting is applied to expressions given to the
721 <methodname>where()</methodname> or <methodname>orWhere()</methodname> methods.
722 If you have column names that need to be quoted, you must use
723 <methodname>quoteIdentifier()</methodname> as you form the string for the
729 The second argument to the <methodname>where()</methodname> method is optional. It
730 is a value to substitute into the expression. <classname>Zend_Db_Select</classname>
731 quotes the value and substitutes it for a question-mark ("<emphasis>?</emphasis>")
732 symbol in the expression.
735 <example id="zend.db.select.building.where.example-param">
736 <title>Example of a parameter in the where() method</title>
738 <programlisting language="php"><![CDATA[
740 // SELECT product_id, product_name, price
742 // WHERE (price > 100.00)
746 $select = $db->select()
748 array('product_id', 'product_name', 'price'))
749 ->where('price > ?', $minimumPrice);
754 You can pass an array as the second parameter to the
755 <methodname>where()</methodname> method when using the <acronym>SQL</acronym> IN
759 <example id="zend.db.select.building.where.example-array">
760 <title>Example of an array parameter in the where() method</title>
762 <programlisting language="php"><![CDATA[
764 // SELECT product_id, product_name, price
766 // WHERE (product_id IN (1, 2, 3))
768 $productIds = array(1, 2, 3);
770 $select = $db->select()
772 array('product_id', 'product_name', 'price'))
773 ->where('product_id IN (?)', $productIds);
778 You can invoke the <methodname>where()</methodname> method multiple times on the
779 same <classname>Zend_Db_Select</classname> object. The resulting query combines the
780 multiple terms together using <acronym>AND</acronym> between them.
783 <example id="zend.db.select.building.where.example-and">
784 <title>Example of multiple where() methods</title>
786 <programlisting language="php"><![CDATA[
788 // SELECT product_id, product_name, price
790 // WHERE (price > 100.00)
791 // AND (price < 500.00)
796 $select = $db->select()
798 array('product_id', 'product_name', 'price'))
799 ->where('price > ?', $minimumPrice)
800 ->where('price < ?', $maximumPrice);
805 If you need to combine terms together using <acronym>OR</acronym>, use the
806 <methodname>orWhere()</methodname> method. This method is used in the same way as
807 the <methodname>where()</methodname> method, except that the term specified is
808 preceded by <acronym>OR</acronym>, instead of <acronym>AND</acronym>.
811 <example id="zend.db.select.building.where.example-or">
812 <title>Example of the orWhere() method</title>
814 <programlisting language="php"><![CDATA[
816 // SELECT product_id, product_name, price
818 // WHERE (price < 100.00)
819 // OR (price > 500.00)
824 $select = $db->select()
826 array('product_id', 'product_name', 'price'))
827 ->where('price < ?', $minimumPrice)
828 ->orWhere('price > ?', $maximumPrice);
833 <classname>Zend_Db_Select</classname> automatically puts parentheses around each
834 expression you specify using the <methodname>where()</methodname> or
835 <methodname>orWhere()</methodname> methods. This helps to ensure that Boolean
836 operator precedence does not cause unexpected results.
839 <example id="zend.db.select.building.where.example-parens">
840 <title>Example of parenthesizing Boolean expressions</title>
842 <programlisting language="php"><![CDATA[
844 // SELECT product_id, product_name, price
846 // WHERE (price < 100.00 OR price > 500.00)
847 // AND (product_name = 'Apple')
853 $select = $db->select()
855 array('product_id', 'product_name', 'price'))
856 ->where("price < $minimumPrice OR price > $maximumPrice")
857 ->where('product_name = ?', $prod);
862 In the example above, the results would be quite different without the parentheses,
863 because <acronym>AND</acronym> has higher precedence than <acronym>OR</acronym>.
864 <classname>Zend_Db_Select</classname> applies the parentheses so the effect is that
865 each expression in successive calls to the <methodname>where()</methodname> bind
866 more tightly than the <acronym>AND</acronym> that combines the expressions.
870 <sect3 id="zend.db.select.building.group">
871 <title>Adding a GROUP BY Clause</title>
874 In <acronym>SQL</acronym>, the <command>GROUP BY</command> clause allows you to
875 reduce the rows of a query result set to one row per unique value found in the
876 columns named in the <command>GROUP BY</command> clause.
880 In <classname>Zend_Db_Select</classname>, you can specify the columns to use for
881 calculating the groups of rows using the <methodname>group()</methodname> method.
882 The argument to this method is a column or an array of columns to use in the
883 <command>GROUP BY</command> clause.
886 <example id="zend.db.select.building.group.example">
887 <title>Example of the group() method</title>
889 <programlisting language="php"><![CDATA[
891 // SELECT p."product_id", COUNT(*) AS line_items_per_product
892 // FROM "products" AS p JOIN "line_items" AS l
893 // ON p.product_id = l.product_id
894 // GROUP BY p.product_id
896 $select = $db->select()
897 ->from(array('p' => 'products'),
899 ->join(array('l' => 'line_items'),
900 'p.product_id = l.product_id',
901 array('line_items_per_product' => 'COUNT(*)'))
902 ->group('p.product_id');
907 Like the columns array in the <methodname>from()</methodname> method, you can use
908 correlation names in the column name strings, and the column is quoted as an
909 identifier unless the string contains parentheses or is an object of type
910 <classname>Zend_Db_Expr</classname>.
914 <sect3 id="zend.db.select.building.having">
915 <title>Adding a HAVING Clause</title>
918 In <acronym>SQL</acronym>, the <constant>HAVING</constant> clause applies a
919 restriction condition on groups of rows. This is similar to how a
920 <constant>WHERE</constant> clause applies a restriction condition on rows. But the
921 two clauses are different because <constant>WHERE</constant> conditions are applied
922 before groups are defined, whereas <constant>HAVING</constant> conditions are
923 applied after groups are defined.
927 In <classname>Zend_Db_Select</classname>, you can specify conditions for restricting
928 groups using the <methodname>having()</methodname> method. Its usage is similar to
929 that of the <methodname>where()</methodname> method. The first argument is a string
930 containing a <acronym>SQL</acronym> expression. The optional second argument is a
931 value that is used to replace a positional parameter placeholder in the
932 <acronym>SQL</acronym> expression. Expressions given in multiple invocations of the
933 <methodname>having()</methodname> method are combined using the Boolean
934 <acronym>AND</acronym> operator, or the <acronym>OR</acronym> operator if you
935 use the <methodname>orHaving()</methodname> method.
938 <example id="zend.db.select.building.having.example">
939 <title>Example of the having() method</title>
941 <programlisting language="php"><![CDATA[
943 // SELECT p."product_id", COUNT(*) AS line_items_per_product
944 // FROM "products" AS p JOIN "line_items" AS l
945 // ON p.product_id = l.product_id
946 // GROUP BY p.product_id
947 // HAVING line_items_per_product > 10
949 $select = $db->select()
950 ->from(array('p' => 'products'),
952 ->join(array('l' => 'line_items'),
953 'p.product_id = l.product_id',
954 array('line_items_per_product' => 'COUNT(*)'))
955 ->group('p.product_id')
956 ->having('line_items_per_product > 10');
962 No quoting is applied to expressions given to the
963 <methodname>having()</methodname> or <methodname>orHaving()</methodname>
964 methods. If you have column names that need to be quoted, you must use
965 <methodname>quoteIdentifier()</methodname> as you form the string for the
971 <sect3 id="zend.db.select.building.order">
972 <title>Adding an ORDER BY Clause</title>
975 In <acronym>SQL</acronym>, the <acronym>ORDER</acronym> BY clause specifies one or
976 more columns or expressions by which the result set of a query is sorted. If
977 multiple columns are listed, the secondary columns are used to resolve ties; the
978 sort order is determined by the secondary columns if the preceding columns contain
979 identical values. The default sorting is from least value to greatest value. You can
980 also sort by greatest value to least value for a given column in the list by
981 specifying the keyword <constant>DESC</constant> after that column.
985 In <classname>Zend_Db_Select</classname>, you can use the
986 <methodname>order()</methodname> method to specify a column or an array of columns
987 by which to sort. Each element of the array is a string naming a column. Optionally
988 with the <constant>ASC</constant> <constant>DESC</constant> keyword following it,
989 separated by a space.
993 Like in the <methodname>from()</methodname> and <methodname>group()</methodname>
994 methods, column names are quoted as identifiers, unless they contain parentheses or
995 are an object of type <classname>Zend_Db_Expr</classname>.
998 <example id="zend.db.select.building.order.example">
999 <title>Example of the order() method</title>
1001 <programlisting language="php"><![CDATA[
1002 // Build this query:
1003 // SELECT p."product_id", COUNT(*) AS line_items_per_product
1004 // FROM "products" AS p JOIN "line_items" AS l
1005 // ON p.product_id = l.product_id
1006 // GROUP BY p.product_id
1007 // ORDER BY "line_items_per_product" DESC, "product_id"
1009 $select = $db->select()
1010 ->from(array('p' => 'products'),
1011 array('product_id'))
1012 ->join(array('l' => 'line_items'),
1013 'p.product_id = l.product_id',
1014 array('line_items_per_product' => 'COUNT(*)'))
1015 ->group('p.product_id')
1016 ->order(array('line_items_per_product DESC',
1018 ]]></programlisting>
1022 <sect3 id="zend.db.select.building.limit">
1023 <title>Adding a LIMIT Clause</title>
1026 Some <acronym>RDBMS</acronym> brands extend <acronym>SQL</acronym> with a query
1027 clause known as the <constant>LIMIT</constant> clause. This clause reduces the
1028 number of rows in the result set to at most a number you specify. You can also
1029 specify to skip a number of rows before starting to output. This feature makes it
1030 easy to take a subset of a result set, for example when displaying query results on
1031 progressive pages of output.
1035 In <classname>Zend_Db_Select</classname>, you can use the
1036 <methodname>limit()</methodname> method to specify the count of rows and the number
1037 of rows to skip. The <emphasis>first</emphasis> argument to this method is the
1038 desired count of rows. The <emphasis>second</emphasis> argument is the number of
1042 <example id="zend.db.select.building.limit.example">
1043 <title>Example of the limit() method</title>
1045 <programlisting language="php"><![CDATA[
1046 // Build this query:
1047 // SELECT p."product_id", p."product_name"
1048 // FROM "products" AS p
1051 // SELECT p."product_id", p."product_name"
1052 // FROM "products" AS p
1053 // LIMIT 20 OFFSET 10
1055 $select = $db->select()
1056 ->from(array('p' => 'products'),
1057 array('product_id', 'product_name'))
1059 ]]></programlisting>
1064 The <constant>LIMIT</constant> syntax is not supported by all
1065 <acronym>RDBMS</acronym> brands. Some <acronym>RDBMS</acronym> require different
1066 syntax to support similar functionality. Each
1067 <classname>Zend_Db_Adapter_Abstract</classname> class includes a method to
1068 produce <acronym>SQL</acronym> appropriate for that <acronym>RDBMS</acronym>.
1073 Use the <methodname>limitPage()</methodname> method for an alternative way to
1074 specify row count and offset. This method allows you to limit the result set to one
1075 of a series of fixed-length subsets of rows from the query's total result set. In
1076 other words, you specify the length of a "page" of results, and the ordinal number
1077 of the single page of results you want the query to return. The page number is the
1078 first argument of the <methodname>limitPage()</methodname> method, and the page
1079 length is the second argument. Both arguments are required; they have no default
1083 <example id="zend.db.select.building.limit.example2">
1084 <title>Example of the limitPage() method</title>
1086 <programlisting language="php"><![CDATA[
1087 // Build this query:
1088 // SELECT p."product_id", p."product_name"
1089 // FROM "products" AS p
1092 $select = $db->select()
1093 ->from(array('p' => 'products'),
1094 array('product_id', 'product_name'))
1096 ]]></programlisting>
1100 <sect3 id="zend.db.select.building.distinct">
1101 <title>Adding the DISTINCT Query Modifier</title>
1104 The <methodname>distinct()</methodname> method enables you to add the
1105 <constant>DISTINCT</constant> keyword to your <acronym>SQL</acronym> query.
1108 <example id="zend.db.select.building.distinct.example">
1109 <title>Example of the distinct() method</title>
1111 <programlisting language="php"><![CDATA[
1112 // Build this query:
1113 // SELECT DISTINCT p."product_name"
1114 // FROM "products" AS p
1116 $select = $db->select()
1118 ->from(array('p' => 'products'), 'product_name');
1119 ]]></programlisting>
1123 <sect3 id="zend.db.select.building.for-update">
1124 <title>Adding the FOR UPDATE Query Modifier</title>
1127 The <methodname>forUpdate()</methodname> method enables you to add the
1128 <acronym>FOR</acronym> <acronym>UPDATE</acronym> modifier to your
1129 <acronym>SQL</acronym> query.
1132 <example id="zend.db.select.building.for-update.example">
1133 <title>Example of forUpdate() method</title>
1135 <programlisting language="php"><![CDATA[
1136 // Build this query:
1137 // SELECT FOR UPDATE p.*
1138 // FROM "products" AS p
1140 $select = $db->select()
1142 ->from(array('p' => 'products'));
1143 ]]></programlisting>
1147 <sect3 id="zend.db.select.building.union">
1148 <title>Building a UNION Query</title>
1151 You can build union queries with <classname>Zend_Db_Select</classname> by passing an
1152 array of <classname>Zend_Db_Select</classname> or <acronym>SQL</acronym> Query
1153 strings into the <methodname>union()</methodname> method. As second parameter you
1154 can pass the <constant>Zend_Db_Select::SQL_UNION</constant> or
1155 <constant>Zend_Db_Select::SQL_UNION_ALL</constant> constants to specify which type
1156 of union you want to perform.
1159 <example id="zend.db.select.building.union.example">
1160 <title>Example of union() method</title>
1162 <programlisting language="php"><![CDATA[
1163 $sql1 = $db->select();
1164 $sql2 = "SELECT ...";
1166 $select = $db->select()
1167 ->union(array($sql1, $sql2))
1169 ]]></programlisting>
1174 <sect2 id="zend.db.select.execute">
1175 <title>Executing Select Queries</title>
1178 This section describes how to execute the query represented by a
1179 <classname>Zend_Db_Select</classname> object.
1182 <sect3 id="zend.db.select.execute.query-adapter">
1183 <title>Executing Select Queries from the Db Adapter</title>
1186 You can execute the query represented by the <classname>Zend_Db_Select</classname>
1187 object by passing it as the first argument to the <methodname>query()</methodname>
1188 method of a <classname>Zend_Db_Adapter_Abstract</classname> object. Use the
1189 <classname>Zend_Db_Select</classname> objects instead of a string query.
1193 The <methodname>query()</methodname> method returns an object of type
1194 <classname>Zend_Db_Statement</classname> or PDOStatement, depending on the adapter
1198 <example id="zend.db.select.execute.query-adapter.example">
1199 <title>Example using the Db adapter's query() method</title>
1201 <programlisting language="php"><![CDATA[
1202 $select = $db->select()
1205 $stmt = $db->query($select);
1206 $result = $stmt->fetchAll();
1207 ]]></programlisting>
1211 <sect3 id="zend.db.select.execute.query-select">
1212 <title>Executing Select Queries from the Object</title>
1215 As an alternative to using the <methodname>query()</methodname> method of the
1216 adapter object, you can use the <methodname>query()</methodname> method of the
1217 <classname>Zend_Db_Select</classname> object. Both methods return an object of type
1218 <classname>Zend_Db_Statement</classname> or PDOStatement, depending on the adapter
1222 <example id="zend.db.select.execute.query-select.example">
1223 <title>Example using the Select object's query method</title>
1225 <programlisting language="php"><![CDATA[
1226 $select = $db->select()
1229 $stmt = $select->query();
1230 $result = $stmt->fetchAll();
1231 ]]></programlisting>
1235 <sect3 id="zend.db.select.execute.tostring">
1236 <title>Converting a Select Object to a SQL String</title>
1239 If you need access to a string representation of the <acronym>SQL</acronym> query
1240 corresponding to the <classname>Zend_Db_Select</classname> object, use the
1241 <methodname>__toString()</methodname> method.
1244 <example id="zend.db.select.execute.tostring.example">
1245 <title>Example of the __toString() method</title>
1247 <programlisting language="php"><![CDATA[
1248 $select = $db->select()
1251 $sql = $select->__toString();
1254 // The output is the string:
1255 // SELECT * FROM "products"
1256 ]]></programlisting>
1261 <sect2 id="zend.db.select.other">
1262 <title>Other methods</title>
1265 This section describes other methods of the <classname>Zend_Db_Select</classname> class
1266 that are not covered above: <methodname>getPart()</methodname> and
1267 <methodname>reset()</methodname>.
1270 <sect3 id="zend.db.select.other.get-part">
1271 <title>Retrieving Parts of the Select Object</title>
1274 The <methodname>getPart()</methodname> method returns a representation of one part
1275 of your <acronym>SQL</acronym> query. For example, you can use this method to return
1276 the array of expressions for the <constant>WHERE</constant> clause, or the array of
1277 columns (or column expressions) that are in the <constant>SELECT</constant> list, or
1278 the values of the count and offset for the <constant>LIMIT</constant> clause.
1282 The return value is not a string containing a fragment of <acronym>SQL</acronym>
1283 syntax. The return value is an internal representation, which is typically an array
1284 structure containing values and expressions. Each part of the query has a different
1289 The single argument to the <methodname>getPart()</methodname> method is a string
1290 that identifies which part of the Select query to return. For example, the string
1291 <command>'from'</command> identifies the part of the Select object that stores
1292 information about the tables in the <constant>FROM</constant> clause, including
1297 The <classname>Zend_Db_Select</classname> class defines constants you can use for
1298 parts of the <acronym>SQL</acronym> query. You can use these constant definitions,
1299 or you can the literal strings.
1302 <table id="zend.db.select.other.get-part.table">
1303 <title>Constants used by getPart() and reset()</title>
1308 <entry>Constant</entry>
1309 <entry>String value</entry>
1315 <entry><constant>Zend_Db_Select::DISTINCT</constant></entry>
1316 <entry><command>'distinct'</command></entry>
1320 <entry><constant>Zend_Db_Select::FOR_UPDATE</constant></entry>
1321 <entry><command>'forupdate'</command></entry>
1325 <entry><constant>Zend_Db_Select::COLUMNS</constant></entry>
1326 <entry><command>'columns'</command></entry>
1330 <entry><constant>Zend_Db_Select::FROM</constant></entry>
1331 <entry><command>'from'</command></entry>
1335 <entry><constant>Zend_Db_Select::WHERE</constant></entry>
1336 <entry><command>'where'</command></entry>
1340 <entry><constant>Zend_Db_Select::GROUP</constant></entry>
1341 <entry><command>'group'</command></entry>
1345 <entry><constant>Zend_Db_Select::HAVING</constant></entry>
1346 <entry><command>'having'</command></entry>
1350 <entry><constant>Zend_Db_Select::ORDER</constant></entry>
1351 <entry><command>'order'</command></entry>
1355 <entry><constant>Zend_Db_Select::LIMIT_COUNT</constant></entry>
1356 <entry><command>'limitcount'</command></entry>
1360 <entry><constant>Zend_Db_Select::LIMIT_OFFSET</constant></entry>
1361 <entry><command>'limitoffset'</command></entry>
1367 <example id="zend.db.select.other.get-part.example">
1368 <title>Example of the getPart() method</title>
1370 <programlisting language="php"><![CDATA[
1371 $select = $db->select()
1373 ->order('product_id');
1375 // You can use a string literal to specify the part
1376 $orderData = $select->getPart( 'order' );
1378 // You can use a constant to specify the same part
1379 $orderData = $select->getPart( Zend_Db_Select::ORDER );
1381 // The return value may be an array structure, not a string.
1382 // Each part has a different structure.
1383 print_r( $orderData );
1384 ]]></programlisting>
1388 <sect3 id="zend.db.select.other.reset">
1389 <title>Resetting Parts of the Select Object</title>
1392 The <methodname>reset()</methodname> method enables you to clear one specified part
1393 of the <acronym>SQL</acronym> query, or else clear all parts of the
1394 <acronym>SQL</acronym> query if you omit the argument.
1398 The single argument is optional. You can specify the part of the query to clear,
1399 using the same strings you used in the argument to the
1400 <methodname>getPart()</methodname> method. The part of the query you specify is
1401 reset to a default state.
1405 If you omit the parameter, <methodname>reset()</methodname> changes all parts of the
1406 query to their default state. This makes the <classname>Zend_Db_Select</classname>
1407 object equivalent to a new object, as though you had just instantiated it.
1410 <example id="zend.db.select.other.reset.example">
1411 <title>Example of the reset() method</title>
1413 <programlisting language="php"><![CDATA[
1414 // Build this query:
1416 // FROM "products" AS p
1417 // ORDER BY "product_name"
1419 $select = $db->select()
1420 ->from(array('p' => 'products')
1421 ->order('product_name');
1423 // Changed requirement, instead order by a different columns:
1425 // FROM "products" AS p
1426 // ORDER BY "product_id"
1428 // Clear one part so we can redefine it
1429 $select->reset( Zend_Db_Select::ORDER );
1431 // And specify a different column
1432 $select->order('product_id');
1434 // Clear all parts of the query
1436 ]]></programlisting>
1443 vim:se ts=4 sw=4 et: