2
0

Zend_Db_Select.xml 55 KB

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