Zend_Db_Select.xml 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.select">
  4. <title>Zend_Db_Select</title>
  5. <sect2 id="zend.db.select.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <classname>Zend_Db_Select</classname> object represents a SQL <code>SELECT</code> query statement. The
  9. class has methods for adding individual parts to the query. You can specify some
  10. parts of the query using PHP methods and data structures, and the class forms the
  11. correct SQL syntax for you. After you build a query, you can execute the query as if
  12. you had written it as a string.
  13. </para>
  14. <para>
  15. The value offered by <classname>Zend_Db_Select</classname> includes:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. Object-oriented methods for specifying SQL queries in a piece-by-piece manner;
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. Database-independent abstraction of some parts of the SQL query;
  26. </para>
  27. </listitem>
  28. <listitem>
  29. <para>
  30. Automatic quoting of metadata identifiers in most cases, to support identifiers
  31. containing SQL reserved words and special characters;
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. Quoting identifiers and values, to help reduce risk of SQL injection attacks.
  37. </para>
  38. </listitem>
  39. </itemizedlist>
  40. <para>
  41. Using <classname>Zend_Db_Select</classname> is not mandatory. For very simple SELECT queries, it is usually
  42. simpler to specify the entire SQL query as a string and execute it using Adapter
  43. methods like <code>query()</code> or <code>fetchAll()</code>. Using <classname>Zend_Db_Select</classname> is
  44. helpful if you need to assemble a SELECT query procedurally, or based on conditional
  45. logic in your application.
  46. </para>
  47. </sect2>
  48. <sect2 id="zend.db.select.creating">
  49. <title>Creating a Select Object</title>
  50. <para>
  51. You can create an instance of a <classname>Zend_Db_Select</classname> object using the <code>select()</code>
  52. method of a <classname>Zend_Db_Adapter_Abstract</classname> object.
  53. </para>
  54. <example id="zend.db.select.creating.example-db">
  55. <title>Example of the database adapter's select() method</title>
  56. <programlisting role="php"><![CDATA[
  57. $db = Zend_Db::factory( ...options... );
  58. $select = $db->select();
  59. ]]></programlisting>
  60. </example>
  61. <para>
  62. Another way to create a <classname>Zend_Db_Select</classname> object is with its constructor, specifying the
  63. database adapter as an argument.
  64. </para>
  65. <example id="zend.db.select.creating.example-new">
  66. <title>Example of creating a new Select object</title>
  67. <programlisting role="php"><![CDATA[
  68. $db = Zend_Db::factory( ...options... );
  69. $select = new Zend_Db_Select($db);
  70. ]]></programlisting>
  71. </example>
  72. </sect2>
  73. <sect2 id="zend.db.select.building">
  74. <title>Building Select queries</title>
  75. <para>
  76. When building the query, you can add clauses of the query one by one. There is a
  77. separate method to add each clause to the <classname>Zend_Db_Select</classname> object.
  78. </para>
  79. <example id="zend.db.select.building.example">
  80. <title>Example of the using methods to add clauses</title>
  81. <programlisting role="php"><![CDATA[
  82. // Create the Zend_Db_Select object
  83. $select = $db->select();
  84. // Add a FROM clause
  85. $select->from( ...specify table and columns... )
  86. // Add a WHERE clause
  87. $select->where( ...specify search criteria... )
  88. // Add an ORDER BY clause
  89. $select->order( ...specify sorting criteria... );
  90. ]]></programlisting>
  91. </example>
  92. <para>
  93. You also can use most methods of the <classname>Zend_Db_Select</classname> object with a convenient fluent
  94. interface. A fluent interface means that each method returns a reference to the object
  95. on which it was called, so you can immediately call another method.
  96. </para>
  97. <example id="zend.db.select.building.example-fluent">
  98. <title>Example of the using the fluent interface</title>
  99. <programlisting role="php"><![CDATA[
  100. $select = $db->select()
  101. ->from( ...specify table and columns... )
  102. ->where( ...specify search criteria... )
  103. ->order( ...specify sorting criteria... );
  104. ]]></programlisting>
  105. </example>
  106. <para>
  107. The examples in this section show usage of the fluent interface, but you can use the
  108. non-fluent interface in all cases. It is often necessary to use the non-fluent
  109. interface, for example, if your application needs to perform some logic before adding a
  110. clause to a query.
  111. </para>
  112. <sect3 id="zend.db.select.building.from">
  113. <title>Adding a FROM clause</title>
  114. <para>
  115. Specify the table for this query using the <code>from()</code> method. You can
  116. specify the table name as a simple string. <classname>Zend_Db_Select</classname> applies identifier
  117. quoting around the table name, so you can use special characters.
  118. </para>
  119. <example id="zend.db.select.building.from.example">
  120. <title>Example of the from() method</title>
  121. <programlisting role="php"><![CDATA[
  122. // Build this query:
  123. // SELECT *
  124. // FROM "products"
  125. $select = $db->select()
  126. ->from( 'products' );
  127. ]]></programlisting>
  128. </example>
  129. <para>
  130. You can also specify the correlation name (sometimes called the "table alias") for
  131. a table. Instead of a simple string, use an associative array mapping the
  132. correlation name to the table name. In other clauses of the SQL query, use this
  133. correlation name. If your query joins more than one table, <classname>Zend_Db_Select</classname> generates
  134. unique correlation names based on the table names, for any tables for which you
  135. don't specify the correlation name.
  136. </para>
  137. <example id="zend.db.select.building.from.example-cname">
  138. <title>Example of specifying a table correlation name</title>
  139. <programlisting role="php"><![CDATA[
  140. // Build this query:
  141. // SELECT p.*
  142. // FROM "products" AS p
  143. $select = $db->select()
  144. ->from( array('p' => 'products') );
  145. ]]></programlisting>
  146. </example>
  147. <para>
  148. Some RDBMS brands support a leading schema specifier for a table. You can specify
  149. the table name as "<code>schemaName.tableName</code>", where <classname>Zend_Db_Select</classname> quotes
  150. each part individually, or you may specify the schema name separately. A schema
  151. name specified in the table name takes precedence over a schema provided separately
  152. in the event that both are provided.
  153. </para>
  154. <example id="zend.db.select.building.from.example-schema">
  155. <title>Example of specifying a schema name</title>
  156. <programlisting role="php"><![CDATA[
  157. // Build this query:
  158. // SELECT *
  159. // FROM "myschema"."products"
  160. $select = $db->select()
  161. ->from( 'myschema.products' );
  162. // or
  163. $select = $db->select()
  164. ->from('products', '*', 'myschema');
  165. ]]></programlisting>
  166. </example>
  167. </sect3>
  168. <sect3 id="zend.db.select.building.columns">
  169. <title>Adding Columns</title>
  170. <para>
  171. In the second argument of the <code>from()</code> method, you can specify the
  172. columns to select from the respective table. If you specify no columns, the default
  173. is "<code>*</code>", the SQL wildcard for "all columns".
  174. </para>
  175. <para>
  176. You can list the columns in a simple array of strings, or as an associative mapping
  177. of column alias to column name. If you only have one column to query, and you don't
  178. need to specify a column alias, you can list it as a plain string instead of an
  179. array.
  180. </para>
  181. <para>
  182. If you give an empty array as the columns argument, no columns from the respective
  183. table are included in the result set. See a
  184. <link linkend="zend.db.select.building.join.example-no-columns">code example</link>
  185. under the section on the <code>join()</code> method.
  186. </para>
  187. <para>
  188. You can specify the column name as "<code>correlationName.columnName</code>".
  189. <classname>Zend_Db_Select</classname> quotes each part individually. If you don't specify a correlation
  190. name for a column, it uses the correlation name for the table named in the current
  191. <code>from()</code> method.
  192. </para>
  193. <example id="zend.db.select.building.columns.example">
  194. <title>Examples of specifying columns</title>
  195. <programlisting role="php"><![CDATA[
  196. // Build this query:
  197. // SELECT p."product_id", p."product_name"
  198. // FROM "products" AS p
  199. $select = $db->select()
  200. ->from(array('p' => 'products'),
  201. array('product_id', 'product_name'));
  202. // Build the same query, specifying correlation names:
  203. // SELECT p."product_id", p."product_name"
  204. // FROM "products" AS p
  205. $select = $db->select()
  206. ->from(array('p' => 'products'),
  207. array('p.product_id', 'p.product_name'));
  208. // Build this query with an alias for one column:
  209. // SELECT p."product_id" AS prodno, p."product_name"
  210. // FROM "products" AS p
  211. $select = $db->select()
  212. ->from(array('p' => 'products'),
  213. array('prodno' => 'product_id', 'product_name'));
  214. ]]></programlisting>
  215. </example>
  216. </sect3>
  217. <sect3 id="zend.db.select.building.columns-expr">
  218. <title>Adding Expression Columns</title>
  219. <para>
  220. Columns in SQL queries are sometimes expressions, not simply column names from a
  221. table. Expressions should not have correlation names or quoting applied. If your
  222. column string contains parentheses, <classname>Zend_Db_Select</classname> recognizes it as an expression.
  223. </para>
  224. <para>
  225. You also can create an object of type <classname>Zend_Db_Expr</classname> explicitly, to prevent a string
  226. from being treated as a column name. <classname>Zend_Db_Expr</classname> is a minimal class that contains
  227. a single string. <classname>Zend_Db_Select</classname> recognizes objects of type <classname>Zend_Db_Expr</classname> and
  228. converts them back to string, but does not apply any alterations, such as quoting
  229. or correlation names.
  230. </para>
  231. <note>
  232. <para>
  233. Using <classname>Zend_Db_Expr</classname> for column names is not necessary if your column expression
  234. contains parentheses; <classname>Zend_Db_Select</classname> recognizes parentheses and treats the
  235. string as an expression, skipping quoting and correlation names.
  236. </para>
  237. </note>
  238. <example id="zend.db.select.building.columns-expr.example">
  239. <title>Examples of specifying columns containing expressions</title>
  240. <programlisting role="php"><![CDATA[
  241. // Build this query:
  242. // SELECT p."product_id", LOWER(product_name)
  243. // FROM "products" AS p
  244. // An expression with parentheses implicitly becomes
  245. // a Zend_Db_Expr.
  246. $select = $db->select()
  247. ->from(array('p' => 'products'),
  248. array('product_id', 'LOWER(product_name)'));
  249. // Build this query:
  250. // SELECT p."product_id", (p.cost * 1.08) AS cost_plus_tax
  251. // FROM "products" AS p
  252. $select = $db->select()
  253. ->from(array('p' => 'products'),
  254. array('product_id',
  255. 'cost_plus_tax' => '(p.cost * 1.08)')
  256. );
  257. // Build this query using Zend_Db_Expr explicitly:
  258. // SELECT p."product_id", p.cost * 1.08 AS cost_plus_tax
  259. // FROM "products" AS p
  260. $select = $db->select()
  261. ->from(array('p' => 'products'),
  262. array('product_id',
  263. 'cost_plus_tax' =>
  264. new Zend_Db_Expr('p.cost * 1.08'))
  265. );
  266. ]]></programlisting>
  267. </example>
  268. <para>
  269. In the cases above, <classname>Zend_Db_Select</classname> does not alter the string to apply correlation
  270. names or identifier quoting. If those changes are necessary to resolve ambiguity,
  271. you must make the changes manually in the string.
  272. </para>
  273. <para>
  274. If your column names are SQL keywords or contain special characters, you should use
  275. the Adapter's <code>quoteIdentifier()</code> method and interpolate the result into
  276. the string. The <code>quoteIdentifier()</code> method uses SQL quoting to delimit
  277. the identifier, which makes it clear that it is an identifier for a table or a
  278. column, and not any other part of SQL syntax.
  279. </para>
  280. <para>
  281. Your code is more database-independent if you use the
  282. <code>quoteIdentifier()</code> method instead of typing quotes literally in your
  283. string, because some RDBMS brands use nonstandard symbols for quoting identifiers.
  284. The <code>quoteIdentifier()</code> method is designed to use the appropriate
  285. quoting symbols based on the adapter type. The <code>quoteIdentifier()</code>
  286. method also escapes any quote characters that appear within the identifier name
  287. itself.
  288. </para>
  289. <example id="zend.db.select.building.columns-quoteid.example">
  290. <title>Examples of quoting columns in an expression</title>
  291. <programlisting role="php"><![CDATA[
  292. // Build this query,
  293. // quoting the special column name "from" in the expression:
  294. // SELECT p."from" + 10 AS origin
  295. // FROM "products" AS p
  296. $select = $db->select()
  297. ->from(array('p' => 'products'),
  298. array('origin' =>
  299. '(p.' . $db->quoteIdentifier('from') . ' + 10)')
  300. );
  301. ]]></programlisting>
  302. </example>
  303. </sect3>
  304. <sect3 id="zend.db.select.building.columns-atomic">
  305. <title>Adding columns to an existing FROM or JOIN table</title>
  306. <para>
  307. There may be cases where you wish to add columns to an existing FROM or JOIN table
  308. after those methods have been called. The <code>columns()</code> method allows you
  309. to add specific columns at any point before the query is executed. You can supply
  310. the columns as either a string or <classname>Zend_Db_Expr</classname> or as an array of these
  311. elements. The second argument to this method can be omitted, implying that the
  312. columns are to be added to the FROM table, otherwise an existing correlation name
  313. must be used.
  314. </para>
  315. <example id="zend.db.select.building.columns-atomic.example">
  316. <title>Examples of adding columns with the columns() method</title>
  317. <programlisting role="php"><![CDATA[
  318. // Build this query:
  319. // SELECT p."product_id", p."product_name"
  320. // FROM "products" AS p
  321. $select = $db->select()
  322. ->from(array('p' => 'products'), 'product_id')
  323. ->columns('product_name');
  324. // Build the same query, specifying correlation names:
  325. // SELECT p."product_id", p."product_name"
  326. // FROM "products" AS p
  327. $select = $db->select()
  328. ->from(array('p' => 'products'), 'p.product_id')
  329. ->columns('product_name', 'p');
  330. // Alternatively use columns('p.product_name')]]></programlisting>
  331. </example>
  332. </sect3>
  333. <sect3 id="zend.db.select.building.join">
  334. <title>Adding Another Table to the Query with JOIN</title>
  335. <para>
  336. Many useful queries involve using a <code>JOIN</code> to combine rows from multiple
  337. tables. You can add tables to a <classname>Zend_Db_Select</classname> query using the <code>join()</code>
  338. method. Using this method is similar to the <code>from()</code> method, except you
  339. can also specify a join condition in most cases.
  340. </para>
  341. <example id="zend.db.select.building.join.example">
  342. <title>Example of the join() method</title>
  343. <programlisting role="php"><![CDATA[
  344. // Build this query:
  345. // SELECT p."product_id", p."product_name", l.*
  346. // FROM "products" AS p JOIN "line_items" AS l
  347. // ON p.product_id = l.product_id
  348. $select = $db->select()
  349. ->from(array('p' => 'products'),
  350. array('product_id', 'product_name'))
  351. ->join(array('l' => 'line_items'),
  352. 'p.product_id = l.product_id');
  353. ]]></programlisting>
  354. </example>
  355. <para>
  356. The second argument to <code>join()</code> is a string that is the join condition.
  357. This is an expression that declares the criteria by which rows in one table match
  358. rows in the the other table. You can use correlation names in this expression.
  359. </para>
  360. <note>
  361. <para>
  362. No quoting is applied to the expression you specify for the join condition; if
  363. you have column names that need to be quoted, you must use
  364. <code>quoteIdentifier()</code> as you form the string for the join condition.
  365. </para>
  366. </note>
  367. <para>
  368. The third argument to <code>join()</code> is an array of column names, like that
  369. used in the <code>from()</code> method. It defaults to "<code>*</code>", supports
  370. correlation names, expressions, and <classname>Zend_Db_Expr</classname> in the same way as the array of
  371. column names in the <code>from()</code> method.
  372. </para>
  373. <para>
  374. To select no columns from a table, use an empty array for the list of columns. This
  375. usage works in the <code>from()</code> method too, but typically you want some
  376. columns from the primary table in your queries, whereas you might want no columns
  377. from a joined table.
  378. </para>
  379. <example id="zend.db.select.building.join.example-no-columns">
  380. <title>Example of specifying no columns</title>
  381. <programlisting role="php"><![CDATA[
  382. // Build this query:
  383. // SELECT p."product_id", p."product_name"
  384. // FROM "products" AS p JOIN "line_items" AS l
  385. // ON p.product_id = l.product_id
  386. $select = $db->select()
  387. ->from(array('p' => 'products'),
  388. array('product_id', 'product_name'))
  389. ->join(array('l' => 'line_items'),
  390. 'p.product_id = l.product_id',
  391. array() ); // empty list of columns
  392. ]]></programlisting>
  393. <para>
  394. Note the empty <code>array()</code> in the above example in place of a list of
  395. columns from the joined table.
  396. </para>
  397. </example>
  398. <para>
  399. SQL has several types of joins. See the list below for the methods to support
  400. different join types in <classname>Zend_Db_Select</classname>.
  401. </para>
  402. <itemizedlist>
  403. <listitem>
  404. <para>
  405. <command>INNER JOIN</command> with the
  406. <code>join(table, join, [columns])</code> or
  407. <code>joinInner(table, join, [columns])</code> methods.
  408. </para>
  409. <para>
  410. This may be the most common type of join. Rows from each table are compared
  411. using the join condition you specify. The result set includes only the rows
  412. that satisfy the join condition. The result set can be empty if no rows
  413. satisfy this condition.
  414. </para>
  415. <para>
  416. All RDBMS brands support this join type.
  417. </para>
  418. </listitem>
  419. <listitem>
  420. <para>
  421. <command>LEFT JOIN</command> with the
  422. <code>joinLeft(table, condition, [columns])</code> method.
  423. </para>
  424. <para>
  425. All rows from the left operand table are included, matching rows from the
  426. right operand table included, and the columns from the right operand table
  427. are filled with NULLs if no row exists matching the left table.
  428. </para>
  429. <para>
  430. All RDBMS brands support this join type.
  431. </para>
  432. </listitem>
  433. <listitem>
  434. <para>
  435. <command>RIGHT JOIN</command> with the
  436. <code>joinRight(table, condition, [columns])</code> method.
  437. </para>
  438. <para>
  439. Right outer join is the complement of left outer join. All rows from the
  440. right operand table are included, matching rows from the left operand table
  441. included, and the columns from the left operand table are filled with NULLs
  442. if no row exists matching the right table.
  443. </para>
  444. <para>
  445. Some RDBMS brands don't support this join type, but in general any right
  446. join can be represented as a left join by reversing the order of the
  447. tables.
  448. </para>
  449. </listitem>
  450. <listitem>
  451. <para>
  452. <command>FULL JOIN</command> with the
  453. <code>joinFull(table, condition, [columns])</code> method.
  454. </para>
  455. <para>
  456. A full outer join is like combining a left outer join and a right outer
  457. join. All rows from both tables are included, paired with each other on the
  458. same row of the result set if they satisfy the join condition, and
  459. otherwise paired with NULLs in place of columns from the other table.
  460. </para>
  461. <para>
  462. Some RDBMS brands don't support this join type.
  463. </para>
  464. </listitem>
  465. <listitem>
  466. <para>
  467. <command>CROSS JOIN</command> with the
  468. <code>joinCross(table, [columns])</code> method.
  469. </para>
  470. <para>
  471. A cross join is a Cartesian product. Every row in the first table is
  472. matched to every row in the second table. Therefore the number of rows in
  473. the result set is equal to the product of the number of rows in each table.
  474. You can filter the result set using conditions in a WHERE clause; in this
  475. way a cross join is similar to the old SQL-89 join syntax.
  476. </para>
  477. <para>
  478. The <code>joinCross()</code> method has no parameter to specify the join
  479. condition. Some RDBMS brands don't support this join type.
  480. </para>
  481. </listitem>
  482. <listitem>
  483. <para>
  484. <command>NATURAL JOIN</command> with the
  485. <code>joinNatural(table, [columns])</code> method.
  486. </para>
  487. <para>
  488. A natural join compares any column(s) that appear with the same name in
  489. both tables. The comparison is equality of all the column(s); comparing the
  490. columns using inequality is not a natural join. Only natural inner joins
  491. are supported by this API, even though SQL permits natural outer joins as
  492. well.
  493. </para>
  494. <para>
  495. The <code>joinNatural()</code> method has no parameter to specify the join
  496. condition.
  497. </para>
  498. </listitem>
  499. </itemizedlist>
  500. <para>
  501. In addition to these join methods, you can simplify your queries by using the
  502. JoinUsing methods. Instead of supplying a full condition to your join, you simply
  503. pass the column name on which to join and the <classname>Zend_Db_Select</classname> object completes the
  504. condition for you.
  505. </para>
  506. <example id="zend.db.select.building.joinusing.example">
  507. <title>Example of the joinUsing() method</title>
  508. <programlisting role="php"><![CDATA[
  509. // Build this query:
  510. // SELECT *
  511. // FROM "table1"
  512. // JOIN "table2"
  513. // ON "table1".column1 = "table2".column1
  514. // WHERE column2 = 'foo'
  515. $select = $db->select()
  516. ->from('table1')
  517. ->joinUsing('table2', 'column1')
  518. ->where('column2 = ?', 'foo');
  519. ]]></programlisting>
  520. </example>
  521. <para>
  522. Each of the applicable join methods in the <classname>Zend_Db_Select</classname> component has a
  523. corresponding 'using' method.
  524. </para>
  525. <itemizedlist>
  526. <listitem>
  527. <para>
  528. <code>joinUsing(table, join, [columns])</code> and
  529. <code>joinInnerUsing(table, join, [columns])</code>
  530. </para>
  531. </listitem>
  532. <listitem>
  533. <para>
  534. <code>joinLeftUsing(table, join, [columns])</code>
  535. </para>
  536. </listitem>
  537. <listitem>
  538. <para>
  539. <code>joinRightUsing(table, join, [columns])</code>
  540. </para>
  541. </listitem>
  542. <listitem>
  543. <para>
  544. <code>joinFullUsing(table, join, [columns])</code>
  545. </para>
  546. </listitem>
  547. </itemizedlist>
  548. </sect3>
  549. <sect3 id="zend.db.select.building.where">
  550. <title>Adding a WHERE Clause</title>
  551. <para>
  552. You can specify criteria for restricting rows of the result set using the
  553. <code>where()</code> method. The first argument of this method is a SQL expression,
  554. and this expression is used in a SQL <code>WHERE</code> clause in the query.
  555. </para>
  556. <example id="zend.db.select.building.where.example">
  557. <title>Example of the where() method</title>
  558. <programlisting role="php"><![CDATA[
  559. // Build this query:
  560. // SELECT product_id, product_name, price
  561. // FROM "products"
  562. // WHERE price > 100.00
  563. $select = $db->select()
  564. ->from('products',
  565. array('product_id', 'product_name', 'price'))
  566. ->where('price > 100.00');
  567. ]]></programlisting>
  568. </example>
  569. <note>
  570. <para>
  571. No quoting is applied to expressions given to the <code>where()</code> or
  572. <code>orWhere()</code> methods. If you have column names that need to be
  573. quoted, you must use <code>quoteIdentifier()</code> as you form the string for
  574. the condition.
  575. </para>
  576. </note>
  577. <para>
  578. The second argument to the <code>where()</code> method is optional. It is a value
  579. to substitute into the expression. <classname>Zend_Db_Select</classname> quotes the value and substitutes
  580. it for a question-mark ("<code>?</code>") symbol in the expression.
  581. </para>
  582. <para>
  583. This method accepts only one parameter. If you have an expression into which you
  584. need to substitute multiple variables, you must format the string manually,
  585. interpolating variables and performing quoting yourself.
  586. </para>
  587. <example id="zend.db.select.building.where.example-param">
  588. <title>Example of a parameter in the where() method</title>
  589. <programlisting role="php"><![CDATA[
  590. // Build this query:
  591. // SELECT product_id, product_name, price
  592. // FROM "products"
  593. // WHERE (price > 100.00)
  594. $minimumPrice = 100;
  595. $select = $db->select()
  596. ->from('products',
  597. array('product_id', 'product_name', 'price'))
  598. ->where('price > ?', $minimumPrice);
  599. ]]></programlisting>
  600. </example>
  601. <para>
  602. You can invoke the <code>where()</code> method multiple times on the same
  603. <classname>Zend_Db_Select</classname> object. The resulting query combines the multiple terms together
  604. using <code>AND</code> between them.
  605. </para>
  606. <example id="zend.db.select.building.where.example-and">
  607. <title>Example of multiple where() methods</title>
  608. <programlisting role="php"><![CDATA[
  609. // Build this query:
  610. // SELECT product_id, product_name, price
  611. // FROM "products"
  612. // WHERE (price > 100.00)
  613. // AND (price < 500.00)
  614. $minimumPrice = 100;
  615. $maximumPrice = 500;
  616. $select = $db->select()
  617. ->from('products',
  618. array('product_id', 'product_name', 'price'))
  619. ->where('price > ?', $minimumPrice)
  620. ->where('price < ?', $maximumPrice);
  621. ]]></programlisting>
  622. </example>
  623. <para>
  624. If you need to combine terms together using <code>OR</code>, use the
  625. <code>orWhere()</code> method. This method is used in the same way as the
  626. <code>where()</code> method, except that the term specified is preceded by
  627. <code>OR</code>, instead of <code>AND</code>.
  628. </para>
  629. <example id="zend.db.select.building.where.example-or">
  630. <title>Example of the orWhere() method</title>
  631. <programlisting role="php"><![CDATA[
  632. // Build this query:
  633. // SELECT product_id, product_name, price
  634. // FROM "products"
  635. // WHERE (price < 100.00)
  636. // OR (price > 500.00)
  637. $minimumPrice = 100;
  638. $maximumPrice = 500;
  639. $select = $db->select()
  640. ->from('products',
  641. array('product_id', 'product_name', 'price'))
  642. ->where('price < ?', $minimumPrice)
  643. ->orWhere('price > ?', $maximumPrice);
  644. ]]></programlisting>
  645. </example>
  646. <para>
  647. <classname>Zend_Db_Select</classname> automatically puts parentheses around each expression you specify
  648. using the <code>where()</code> or <code>orWhere()</code> methods. This helps to
  649. ensure that Boolean operator precedence does not cause unexpected results.
  650. </para>
  651. <example id="zend.db.select.building.where.example-parens">
  652. <title>Example of parenthesizing Boolean expressions</title>
  653. <programlisting role="php"><![CDATA[
  654. // Build this query:
  655. // SELECT product_id, product_name, price
  656. // FROM "products"
  657. // WHERE (price < 100.00 OR price > 500.00)
  658. // AND (product_name = 'Apple')
  659. $minimumPrice = 100;
  660. $maximumPrice = 500;
  661. $prod = 'Apple';
  662. $select = $db->select()
  663. ->from('products',
  664. array('product_id', 'product_name', 'price'))
  665. ->where("price < $minimumPrice OR price > $maximumPrice")
  666. ->where('product_name = ?', $prod);
  667. ]]></programlisting>
  668. </example>
  669. <para>
  670. In the example above, the results would be quite different without the parentheses,
  671. because <code>AND</code> has higher precedence than <code>OR</code>. <classname>Zend_Db_Select</classname>
  672. applies the parentheses so the effect is that each expression in successive calls
  673. to the <code>where()</code> bind more tightly than the <code>AND</code> that
  674. combines the expressions.
  675. </para>
  676. </sect3>
  677. <sect3 id="zend.db.select.building.group">
  678. <title>Adding a GROUP BY Clause</title>
  679. <para>
  680. In SQL, the <code>GROUP BY</code> clause allows you to reduce the rows of a query
  681. result set to one row per unique value found in the column(s) named in the
  682. <code>GROUP BY</code> clause.
  683. </para>
  684. <para>
  685. In <classname>Zend_Db_Select</classname>, you can specify the column(s) to use for calculating the groups
  686. of rows using the <code>group()</code> method. The argument to this method is a
  687. column or an array of columns to use in the <code>GROUP BY</code> clause.
  688. </para>
  689. <example id="zend.db.select.building.group.example">
  690. <title>Example of the group() method</title>
  691. <programlisting role="php"><![CDATA[
  692. // Build this query:
  693. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  694. // FROM "products" AS p JOIN "line_items" AS l
  695. // ON p.product_id = l.product_id
  696. // GROUP BY p.product_id
  697. $select = $db->select()
  698. ->from(array('p' => 'products'),
  699. array('product_id'))
  700. ->join(array('l' => 'line_items'),
  701. 'p.product_id = l.product_id',
  702. array('line_items_per_product' => 'COUNT(*)'))
  703. ->group('p.product_id');
  704. ]]></programlisting>
  705. </example>
  706. <para>
  707. Like the columns array in the <code>from()</code> method, you can use correlation
  708. names in the column name strings, and the column is quoted as an identifier unless
  709. the string contains parentheses or is an object of type <classname>Zend_Db_Expr</classname>.
  710. </para>
  711. </sect3>
  712. <sect3 id="zend.db.select.building.having">
  713. <title>Adding a HAVING Clause</title>
  714. <para>
  715. In SQL, the <code>HAVING</code> clause applies a restriction condition on groups of
  716. rows. This is similar to how a <code>WHERE</code> clause applies a restriction
  717. condition on rows. But the two clauses are different because <code>WHERE</code>
  718. conditions are applied before groups are defined, whereas <code>HAVING</code>
  719. conditions are applied after groups are defined.
  720. </para>
  721. <para>
  722. In <classname>Zend_Db_Select</classname>, you can specify conditions for restricting groups using the
  723. <code>having()</code> method. Its usage is similar to that of the
  724. <code>where()</code> method. The first argument is a string containing a SQL
  725. expression. The optional second argument is a value that is used to replace a
  726. positional parameter placeholder in the SQL expression. Expressions given in
  727. multiple invocations of the <code>having()</code> method are combined using the
  728. Boolean <code>AND</code> operator, or the <code>OR</code> operator if you use the
  729. <code>orHaving()</code> method.
  730. </para>
  731. <example id="zend.db.select.building.having.example">
  732. <title>Example of the having() method</title>
  733. <programlisting role="php"><![CDATA[
  734. // Build this query:
  735. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  736. // FROM "products" AS p JOIN "line_items" AS l
  737. // ON p.product_id = l.product_id
  738. // GROUP BY p.product_id
  739. // HAVING line_items_per_product > 10
  740. $select = $db->select()
  741. ->from(array('p' => 'products'),
  742. array('product_id'))
  743. ->join(array('l' => 'line_items'),
  744. 'p.product_id = l.product_id',
  745. array('line_items_per_product' => 'COUNT(*)'))
  746. ->group('p.product_id')
  747. ->having('line_items_per_product > 10');
  748. ]]></programlisting>
  749. </example>
  750. <note>
  751. <para>
  752. No quoting is applied to expressions given to the <code>having()</code> or
  753. <code>orHaving()</code> methods. If you have column names that need to be
  754. quoted, you must use <code>quoteIdentifier()</code> as you form the string for
  755. the condition.
  756. </para>
  757. </note>
  758. </sect3>
  759. <sect3 id="zend.db.select.building.order">
  760. <title>Adding an ORDER BY Clause</title>
  761. <para>
  762. In SQL, the <code>ORDER BY</code> clause specifies one or more columns or
  763. expressions by which the result set of a query is sorted. If multiple columns are
  764. listed, the secondary columns are used to resolve ties; the sort order is
  765. determined by the secondary columns if the preceding columns contain identical
  766. values. The default sorting is from least value to greatest value. You can also
  767. sort by greatest value to least value for a given column in the list by specifying
  768. the keyword <code>DESC</code> after that column.
  769. </para>
  770. <para>
  771. In <classname>Zend_Db_Select</classname>, you can use the <code>order()</code> method to specify a column
  772. or an array of columns by which to sort. Each element of the array is a string
  773. naming a column. Optionally with the <code>ASC</code> <code>DESC</code> keyword
  774. following it, separated by a space.
  775. </para>
  776. <para>
  777. Like in the <code>from()</code> and <code>group()</code> methods, column names are
  778. quoted as identifiers, unless they contain contain parentheses or are an object of
  779. type <classname>Zend_Db_Expr</classname>.
  780. </para>
  781. <example id="zend.db.select.building.order.example">
  782. <title>Example of the order() method</title>
  783. <programlisting role="php"><![CDATA[
  784. // Build this query:
  785. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  786. // FROM "products" AS p JOIN "line_items" AS l
  787. // ON p.product_id = l.product_id
  788. // GROUP BY p.product_id
  789. // ORDER BY "line_items_per_product" DESC, "product_id"
  790. $select = $db->select()
  791. ->from(array('p' => 'products'),
  792. array('product_id'))
  793. ->join(array('l' => 'line_items'),
  794. 'p.product_id = l.product_id',
  795. array('line_items_per_product' => 'COUNT(*)'))
  796. ->group('p.product_id')
  797. ->order(array('line_items_per_product DESC',
  798. 'product_id'));
  799. ]]></programlisting>
  800. </example>
  801. </sect3>
  802. <sect3 id="zend.db.select.building.limit">
  803. <title>Adding a LIMIT Clause</title>
  804. <para>
  805. Some RDBMS brands extend SQL with a query clause known as the <code>LIMIT</code>
  806. clause. This clause reduces the number of rows in the result set to at most a
  807. number you specify. You can also specify to skip a number of rows before starting
  808. to output. This feature makes it easy to take a subset of a result set, for example
  809. when displaying query results on progressive pages of output.
  810. </para>
  811. <para>
  812. In <classname>Zend_Db_Select</classname>, you can use the <code>limit()</code> method to specify the count
  813. of rows and the number of rows to skip. The first argument to this method is the
  814. desired count of rows. The second argument is the number of rows to skip.
  815. </para>
  816. <example id="zend.db.select.building.limit.example">
  817. <title>Example of the limit() method</title>
  818. <programlisting role="php"><![CDATA[
  819. // Build this query:
  820. // SELECT p."product_id", p."product_name"
  821. // FROM "products" AS p
  822. // LIMIT 10, 20
  823. $select = $db->select()
  824. ->from(array('p' => 'products'),
  825. array('product_id', 'product_name'))
  826. ->limit(10, 20);
  827. ]]></programlisting>
  828. </example>
  829. <note>
  830. <para>
  831. The <code>LIMIT</code> syntax is not supported by all RDBMS brands. Some RDBMS
  832. require different syntax to support similar functionality. Each
  833. <classname>Zend_Db_Adapter_Abstract</classname> class includes a method to produce SQL appropriate for
  834. that RDBMS.
  835. </para>
  836. </note>
  837. <para>
  838. Use the <code>limitPage()</code> method for an alternative way to specify row count
  839. and offset. This method allows you to limit the result set to one of a series of
  840. fixed-length subsets of rows from the query's total result set. In other words, you
  841. specify the length of a "page" of results, and the ordinal number of the single
  842. page of results you want the query to return. The page number is the first argument
  843. of the <code>limitPage()</code> method, and the page length is the second argument.
  844. Both arguments are required; they have no default values.
  845. </para>
  846. <example id="zend.db.select.building.limit.example2">
  847. <title>Example of the limitPage() method</title>
  848. <programlisting role="php"><![CDATA[
  849. // Build this query:
  850. // SELECT p."product_id", p."product_name"
  851. // FROM "products" AS p
  852. // LIMIT 10, 20
  853. $select = $db->select()
  854. ->from(array('p' => 'products'),
  855. array('product_id', 'product_name'))
  856. ->limitPage(2, 10);
  857. ]]></programlisting>
  858. </example>
  859. </sect3>
  860. <sect3 id="zend.db.select.building.distinct">
  861. <title>Adding the DISTINCT Query Modifier</title>
  862. <para>
  863. The <code>distinct()</code> method enables you to add the <code>DISTINCT</code>
  864. keyword to your SQL query.
  865. </para>
  866. <example id="zend.db.select.building.distinct.example">
  867. <title>Example of the distinct() method</title>
  868. <programlisting role="php"><![CDATA[
  869. // Build this query:
  870. // SELECT DISTINCT p."product_name"
  871. // FROM "products" AS p
  872. $select = $db->select()
  873. ->distinct()
  874. ->from(array('p' => 'products'), 'product_name');
  875. ]]></programlisting>
  876. </example>
  877. </sect3>
  878. <sect3 id="zend.db.select.building.for-update">
  879. <title>Adding the FOR UPDATE Query Modifier</title>
  880. <para>
  881. The <code>forUpdate()</code> method enables you to add the <code>FOR UPDATE</code>
  882. modifier to your SQL query.
  883. </para>
  884. <example id="zend.db.select.building.for-update.example">
  885. <title>Example of forUpdate() method</title>
  886. <programlisting role="php"><![CDATA[
  887. // Build this query:
  888. // SELECT FOR UPDATE p.*
  889. // FROM "products" AS p
  890. $select = $db->select()
  891. ->forUpdate()
  892. ->from(array('p' => 'products'));
  893. ]]></programlisting>
  894. </example>
  895. </sect3>
  896. </sect2>
  897. <sect2 id="zend.db.select.execute">
  898. <title>Executing Select Queries</title>
  899. <para>
  900. This section describes how to execute the query represented by a <classname>Zend_Db_Select</classname> object.
  901. </para>
  902. <sect3 id="zend.db.select.execute.query-adapter">
  903. <title>Executing Select Queries from the Db Adapter</title>
  904. <para>
  905. You can execute the query represented by the <classname>Zend_Db_Select</classname> object by passing it as
  906. the first argument to the <code>query()</code> method of a <classname>Zend_Db_Adapter_Abstract</classname>
  907. object. Use the <classname>Zend_Db_Select</classname> objects instead of a string query.
  908. </para>
  909. <para>
  910. The <code>query()</code> method returns an object of type <classname>Zend_Db_Statement</classname> or
  911. PDOStatement, depending on the adapter type.
  912. </para>
  913. <example id="zend.db.select.execute.query-adapter.example">
  914. <title>Example using the Db adapter's query() method</title>
  915. <programlisting role="php"><![CDATA[
  916. $select = $db->select()
  917. ->from('products');
  918. $stmt = $db->query($select);
  919. $result = $stmt->fetchAll();
  920. ]]></programlisting>
  921. </example>
  922. </sect3>
  923. <sect3 id="zend.db.select.execute.query-select">
  924. <title>Executing Select Queries from the Object</title>
  925. <para>
  926. As an alternative to using the <code>query()</code> method of the adapter object,
  927. you can use the <code>query()</code> method of the <classname>Zend_Db_Select</classname> object. Both
  928. methods return an object of type <classname>Zend_Db_Statement</classname> or PDOStatement, depending on
  929. the adapter type.
  930. </para>
  931. <example id="zend.db.select.execute.query-select.example">
  932. <title>Example using the Select object's query method</title>
  933. <programlisting role="php"><![CDATA[
  934. $select = $db->select()
  935. ->from('products');
  936. $stmt = $select->query();
  937. $result = $stmt->fetchAll();
  938. ]]></programlisting>
  939. </example>
  940. </sect3>
  941. <sect3 id="zend.db.select.execute.tostring">
  942. <title>Converting a Select Object to a SQL String</title>
  943. <para>
  944. If you need access to a string representation of the SQL query corresponding to the
  945. <classname>Zend_Db_Select</classname> object, use the <code>__toString()</code> method.
  946. </para>
  947. <example id="zend.db.select.execute.tostring.example">
  948. <title>Example of the __toString() method</title>
  949. <programlisting role="php"><![CDATA[
  950. $select = $db->select()
  951. ->from('products');
  952. $sql = $select->__toString();
  953. echo "$sql\n";
  954. // The output is the string:
  955. // SELECT * FROM "products"
  956. ]]></programlisting>
  957. </example>
  958. </sect3>
  959. </sect2>
  960. <sect2 id="zend.db.select.other">
  961. <title>Other methods</title>
  962. <para>
  963. This section describes other methods of the <classname>Zend_Db_Select</classname> class that are not covered
  964. above: <code>getPart()</code> and <code>reset()</code>.
  965. </para>
  966. <sect3 id="zend.db.select.other.get-part">
  967. <title>Retrieving Parts of the Select Object</title>
  968. <para>
  969. The <code>getPart()</code> method returns a representation of one part of your SQL
  970. query. For example, you can use this method to return the array of expressions for
  971. the <code>WHERE</code> clause, or the array of columns (or column expressions) that
  972. are in the <code>SELECT</code> list, or the values of the count and offset for the
  973. <code>LIMIT</code> clause.
  974. </para>
  975. <para>
  976. The return value is not a string containing a fragment of SQL syntax. The return
  977. value is an internal representation, which is typically an array structure
  978. containing values and expressions. Each part of the query has a different
  979. structure.
  980. </para>
  981. <para>
  982. The single argument to the <code>getPart()</code> method is a string that
  983. identifies which part of the Select query to return. For example, the string
  984. <code>'from'</code> identifies the part of the Select object that stores
  985. information about the tables in the <code>FROM</code> clause, including joined
  986. tables.
  987. </para>
  988. <para>
  989. The <classname>Zend_Db_Select</classname> class defines constants you can use for parts of the SQL query.
  990. You can use these constant definitions, or you can the literal strings.
  991. </para>
  992. <table id="zend.db.select.other.get-part.table">
  993. <title>Constants used by getPart() and reset()</title>
  994. <tgroup cols="2">
  995. <thead>
  996. <row>
  997. <entry>Constant</entry>
  998. <entry>String value</entry>
  999. </row>
  1000. </thead>
  1001. <tbody>
  1002. <row>
  1003. <entry><classname>Zend_Db_Select::DISTINCT</classname></entry>
  1004. <entry><code>'distinct'</code></entry>
  1005. </row>
  1006. <row>
  1007. <entry><classname>Zend_Db_Select::FOR_UPDATE</classname></entry>
  1008. <entry><code>'forupdate'</code></entry>
  1009. </row>
  1010. <row>
  1011. <entry><classname>Zend_Db_Select::COLUMNS</classname></entry>
  1012. <entry><code>'columns'</code></entry>
  1013. </row>
  1014. <row>
  1015. <entry><classname>Zend_Db_Select::FROM</classname></entry>
  1016. <entry><code>'from'</code></entry>
  1017. </row>
  1018. <row>
  1019. <entry><classname>Zend_Db_Select::WHERE</classname></entry>
  1020. <entry><code>'where'</code></entry>
  1021. </row>
  1022. <row>
  1023. <entry><classname>Zend_Db_Select::GROUP</classname></entry>
  1024. <entry><code>'group'</code></entry>
  1025. </row>
  1026. <row>
  1027. <entry><classname>Zend_Db_Select::HAVING</classname></entry>
  1028. <entry><code>'having'</code></entry>
  1029. </row>
  1030. <row>
  1031. <entry><classname>Zend_Db_Select::ORDER</classname></entry>
  1032. <entry><code>'order'</code></entry>
  1033. </row>
  1034. <row>
  1035. <entry><classname>Zend_Db_Select::LIMIT_COUNT</classname></entry>
  1036. <entry><code>'limitcount'</code></entry>
  1037. </row>
  1038. <row>
  1039. <entry><classname>Zend_Db_Select::LIMIT_OFFSET</classname></entry>
  1040. <entry><code>'limitoffset'</code></entry>
  1041. </row>
  1042. </tbody>
  1043. </tgroup>
  1044. </table>
  1045. <example id="zend.db.select.other.get-part.example">
  1046. <title>Example of the getPart() method</title>
  1047. <programlisting role="php"><![CDATA[
  1048. $select = $db->select()
  1049. ->from('products')
  1050. ->order('product_id');
  1051. // You can use a string literal to specify the part
  1052. $orderData = $select->getPart( 'order' );
  1053. // You can use a constant to specify the same part
  1054. $orderData = $select->getPart( Zend_Db_Select::ORDER );
  1055. // The return value may be an array structure, not a string.
  1056. // Each part has a different structure.
  1057. print_r( $orderData );
  1058. ]]></programlisting>
  1059. </example>
  1060. </sect3>
  1061. <sect3 id="zend.db.select.other.reset">
  1062. <title>Resetting Parts of the Select Object</title>
  1063. <para>
  1064. The <code>reset()</code> method enables you to clear one specified part of the SQL
  1065. query, or else clear all parts of the SQL query if you omit the argument.
  1066. </para>
  1067. <para>
  1068. The single argument is optional. You can specify the part of the query to clear,
  1069. using the same strings you used in the argument to the <code>getPart()</code>
  1070. method. The part of the query you specify is reset to a default state.
  1071. </para>
  1072. <para>
  1073. If you omit the parameter, <code>reset()</code> changes all parts of the query to
  1074. their default state. This makes the <classname>Zend_Db_Select</classname> object equivalent to a new
  1075. object, as though you had just instantiated it.
  1076. </para>
  1077. <example id="zend.db.select.other.reset.example">
  1078. <title>Example of the reset() method</title>
  1079. <programlisting role="php"><![CDATA[
  1080. // Build this query:
  1081. // SELECT p.*
  1082. // FROM "products" AS p
  1083. // ORDER BY "product_name"
  1084. $select = $db->select()
  1085. ->from(array('p' => 'products')
  1086. ->order('product_name');
  1087. // Changed requirement, instead order by a different columns:
  1088. // SELECT p.*
  1089. // FROM "products" AS p
  1090. // ORDER BY "product_id"
  1091. // Clear one part so we can redefine it
  1092. $select->reset( Zend_Db_Select::ORDER );
  1093. // And specify a different column
  1094. $select->order('product_id');
  1095. // Clear all parts of the query
  1096. $select->reset();
  1097. ]]></programlisting>
  1098. </example>
  1099. </sect3>
  1100. </sect2>
  1101. </sect1>
  1102. <!--
  1103. vim:se ts=4 sw=4 et:
  1104. -->