Zend_Db_Select.xml 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 language="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 language="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 language="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 language="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 language="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 language="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 language="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 language="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 language="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 language="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 language="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')
  331. ]]></programlisting>
  332. </example>
  333. </sect3>
  334. <sect3 id="zend.db.select.building.join">
  335. <title>Adding Another Table to the Query with JOIN</title>
  336. <para>
  337. Many useful queries involve using a <code>JOIN</code> to combine rows from multiple
  338. tables. You can add tables to a <classname>Zend_Db_Select</classname> query using the <code>join()</code>
  339. method. Using this method is similar to the <code>from()</code> method, except you
  340. can also specify a join condition in most cases.
  341. </para>
  342. <example id="zend.db.select.building.join.example">
  343. <title>Example of the join() method</title>
  344. <programlisting language="php"><![CDATA[
  345. // Build this query:
  346. // SELECT p."product_id", p."product_name", l.*
  347. // FROM "products" AS p JOIN "line_items" AS l
  348. // ON p.product_id = l.product_id
  349. $select = $db->select()
  350. ->from(array('p' => 'products'),
  351. array('product_id', 'product_name'))
  352. ->join(array('l' => 'line_items'),
  353. 'p.product_id = l.product_id');
  354. ]]></programlisting>
  355. </example>
  356. <para>
  357. The second argument to <code>join()</code> is a string that is the join condition.
  358. This is an expression that declares the criteria by which rows in one table match
  359. rows in the the other table. You can use correlation names in this expression.
  360. </para>
  361. <note>
  362. <para>
  363. No quoting is applied to the expression you specify for the join condition; if
  364. you have column names that need to be quoted, you must use
  365. <code>quoteIdentifier()</code> as you form the string for the join condition.
  366. </para>
  367. </note>
  368. <para>
  369. The third argument to <code>join()</code> is an array of column names, like that
  370. used in the <code>from()</code> method. It defaults to "<code>*</code>", supports
  371. correlation names, expressions, and <classname>Zend_Db_Expr</classname> in the same way as the array of
  372. column names in the <code>from()</code> method.
  373. </para>
  374. <para>
  375. To select no columns from a table, use an empty array for the list of columns. This
  376. usage works in the <code>from()</code> method too, but typically you want some
  377. columns from the primary table in your queries, whereas you might want no columns
  378. from a joined table.
  379. </para>
  380. <example id="zend.db.select.building.join.example-no-columns">
  381. <title>Example of specifying no columns</title>
  382. <programlisting language="php"><![CDATA[
  383. // Build this query:
  384. // SELECT p."product_id", p."product_name"
  385. // FROM "products" AS p JOIN "line_items" AS l
  386. // ON p.product_id = l.product_id
  387. $select = $db->select()
  388. ->from(array('p' => 'products'),
  389. array('product_id', 'product_name'))
  390. ->join(array('l' => 'line_items'),
  391. 'p.product_id = l.product_id',
  392. array() ); // empty list of columns
  393. ]]></programlisting>
  394. <para>
  395. Note the empty <code>array()</code> in the above example in place of a list of
  396. columns from the joined table.
  397. </para>
  398. </example>
  399. <para>
  400. SQL has several types of joins. See the list below for the methods to support
  401. different join types in <classname>Zend_Db_Select</classname>.
  402. </para>
  403. <itemizedlist>
  404. <listitem>
  405. <para>
  406. <command>INNER JOIN</command> with the
  407. <code>join(table, join, [columns])</code> or
  408. <code>joinInner(table, join, [columns])</code> methods.
  409. </para>
  410. <para>
  411. This may be the most common type of join. Rows from each table are compared
  412. using the join condition you specify. The result set includes only the rows
  413. that satisfy the join condition. The result set can be empty if no rows
  414. satisfy this condition.
  415. </para>
  416. <para>
  417. All RDBMS brands support this join type.
  418. </para>
  419. </listitem>
  420. <listitem>
  421. <para>
  422. <command>LEFT JOIN</command> with the
  423. <code>joinLeft(table, condition, [columns])</code> method.
  424. </para>
  425. <para>
  426. All rows from the left operand table are included, matching rows from the
  427. right operand table included, and the columns from the right operand table
  428. are filled with NULLs if no row exists matching the left table.
  429. </para>
  430. <para>
  431. All RDBMS brands support this join type.
  432. </para>
  433. </listitem>
  434. <listitem>
  435. <para>
  436. <command>RIGHT JOIN</command> with the
  437. <code>joinRight(table, condition, [columns])</code> method.
  438. </para>
  439. <para>
  440. Right outer join is the complement of left outer join. All rows from the
  441. right operand table are included, matching rows from the left operand table
  442. included, and the columns from the left operand table are filled with NULLs
  443. if no row exists matching the right table.
  444. </para>
  445. <para>
  446. Some RDBMS brands don't support this join type, but in general any right
  447. join can be represented as a left join by reversing the order of the
  448. tables.
  449. </para>
  450. </listitem>
  451. <listitem>
  452. <para>
  453. <command>FULL JOIN</command> with the
  454. <code>joinFull(table, condition, [columns])</code> method.
  455. </para>
  456. <para>
  457. A full outer join is like combining a left outer join and a right outer
  458. join. All rows from both tables are included, paired with each other on the
  459. same row of the result set if they satisfy the join condition, and
  460. otherwise paired with NULLs in place of columns from the other table.
  461. </para>
  462. <para>
  463. Some RDBMS brands don't support this join type.
  464. </para>
  465. </listitem>
  466. <listitem>
  467. <para>
  468. <command>CROSS JOIN</command> with the
  469. <code>joinCross(table, [columns])</code> method.
  470. </para>
  471. <para>
  472. A cross join is a Cartesian product. Every row in the first table is
  473. matched to every row in the second table. Therefore the number of rows in
  474. the result set is equal to the product of the number of rows in each table.
  475. You can filter the result set using conditions in a WHERE clause; in this
  476. way a cross join is similar to the old SQL-89 join syntax.
  477. </para>
  478. <para>
  479. The <code>joinCross()</code> method has no parameter to specify the join
  480. condition. Some RDBMS brands don't support this join type.
  481. </para>
  482. </listitem>
  483. <listitem>
  484. <para>
  485. <command>NATURAL JOIN</command> with the
  486. <code>joinNatural(table, [columns])</code> method.
  487. </para>
  488. <para>
  489. A natural join compares any column(s) that appear with the same name in
  490. both tables. The comparison is equality of all the column(s); comparing the
  491. columns using inequality is not a natural join. Only natural inner joins
  492. are supported by this API, even though SQL permits natural outer joins as
  493. well.
  494. </para>
  495. <para>
  496. The <code>joinNatural()</code> method has no parameter to specify the join
  497. condition.
  498. </para>
  499. </listitem>
  500. </itemizedlist>
  501. <para>
  502. In addition to these join methods, you can simplify your queries by using the
  503. JoinUsing methods. Instead of supplying a full condition to your join, you simply
  504. pass the column name on which to join and the <classname>Zend_Db_Select</classname> object completes the
  505. condition for you.
  506. </para>
  507. <example id="zend.db.select.building.joinusing.example">
  508. <title>Example of the joinUsing() method</title>
  509. <programlisting language="php"><![CDATA[
  510. // Build this query:
  511. // SELECT *
  512. // FROM "table1"
  513. // JOIN "table2"
  514. // ON "table1".column1 = "table2".column1
  515. // WHERE column2 = 'foo'
  516. $select = $db->select()
  517. ->from('table1')
  518. ->joinUsing('table2', 'column1')
  519. ->where('column2 = ?', 'foo');
  520. ]]></programlisting>
  521. </example>
  522. <para>
  523. Each of the applicable join methods in the <classname>Zend_Db_Select</classname> component has a
  524. corresponding 'using' method.
  525. </para>
  526. <itemizedlist>
  527. <listitem>
  528. <para>
  529. <code>joinUsing(table, join, [columns])</code> and
  530. <code>joinInnerUsing(table, join, [columns])</code>
  531. </para>
  532. </listitem>
  533. <listitem>
  534. <para>
  535. <code>joinLeftUsing(table, join, [columns])</code>
  536. </para>
  537. </listitem>
  538. <listitem>
  539. <para>
  540. <code>joinRightUsing(table, join, [columns])</code>
  541. </para>
  542. </listitem>
  543. <listitem>
  544. <para>
  545. <code>joinFullUsing(table, join, [columns])</code>
  546. </para>
  547. </listitem>
  548. </itemizedlist>
  549. </sect3>
  550. <sect3 id="zend.db.select.building.where">
  551. <title>Adding a WHERE Clause</title>
  552. <para>
  553. You can specify criteria for restricting rows of the result set using the
  554. <code>where()</code> method. The first argument of this method is a SQL expression,
  555. and this expression is used in a SQL <code>WHERE</code> clause in the query.
  556. </para>
  557. <example id="zend.db.select.building.where.example">
  558. <title>Example of the where() method</title>
  559. <programlisting language="php"><![CDATA[
  560. // Build this query:
  561. // SELECT product_id, product_name, price
  562. // FROM "products"
  563. // WHERE price > 100.00
  564. $select = $db->select()
  565. ->from('products',
  566. array('product_id', 'product_name', 'price'))
  567. ->where('price > 100.00');
  568. ]]></programlisting>
  569. </example>
  570. <note>
  571. <para>
  572. No quoting is applied to expressions given to the <code>where()</code> or
  573. <code>orWhere()</code> methods. If you have column names that need to be
  574. quoted, you must use <code>quoteIdentifier()</code> as you form the string for
  575. the condition.
  576. </para>
  577. </note>
  578. <para>
  579. The second argument to the <code>where()</code> method is optional. It is a value
  580. to substitute into the expression. <classname>Zend_Db_Select</classname> quotes the value and substitutes
  581. it for a question-mark ("<code>?</code>") symbol in the expression.
  582. </para>
  583. <para>
  584. This method accepts only one parameter. If you have an expression into which you
  585. need to substitute multiple variables, you must format the string manually,
  586. interpolating variables and performing quoting yourself.
  587. </para>
  588. <example id="zend.db.select.building.where.example-param">
  589. <title>Example of a parameter in the where() method</title>
  590. <programlisting language="php"><![CDATA[
  591. // Build this query:
  592. // SELECT product_id, product_name, price
  593. // FROM "products"
  594. // WHERE (price > 100.00)
  595. $minimumPrice = 100;
  596. $select = $db->select()
  597. ->from('products',
  598. array('product_id', 'product_name', 'price'))
  599. ->where('price > ?', $minimumPrice);
  600. ]]></programlisting>
  601. </example>
  602. <para>
  603. You can invoke the <code>where()</code> method multiple times on the same
  604. <classname>Zend_Db_Select</classname> object. The resulting query combines the multiple terms together
  605. using <code>AND</code> between them.
  606. </para>
  607. <example id="zend.db.select.building.where.example-and">
  608. <title>Example of multiple where() methods</title>
  609. <programlisting language="php"><![CDATA[
  610. // Build this query:
  611. // SELECT product_id, product_name, price
  612. // FROM "products"
  613. // WHERE (price > 100.00)
  614. // AND (price < 500.00)
  615. $minimumPrice = 100;
  616. $maximumPrice = 500;
  617. $select = $db->select()
  618. ->from('products',
  619. array('product_id', 'product_name', 'price'))
  620. ->where('price > ?', $minimumPrice)
  621. ->where('price < ?', $maximumPrice);
  622. ]]></programlisting>
  623. </example>
  624. <para>
  625. If you need to combine terms together using <code>OR</code>, use the
  626. <code>orWhere()</code> method. This method is used in the same way as the
  627. <code>where()</code> method, except that the term specified is preceded by
  628. <code>OR</code>, instead of <code>AND</code>.
  629. </para>
  630. <example id="zend.db.select.building.where.example-or">
  631. <title>Example of the orWhere() method</title>
  632. <programlisting language="php"><![CDATA[
  633. // Build this query:
  634. // SELECT product_id, product_name, price
  635. // FROM "products"
  636. // WHERE (price < 100.00)
  637. // OR (price > 500.00)
  638. $minimumPrice = 100;
  639. $maximumPrice = 500;
  640. $select = $db->select()
  641. ->from('products',
  642. array('product_id', 'product_name', 'price'))
  643. ->where('price < ?', $minimumPrice)
  644. ->orWhere('price > ?', $maximumPrice);
  645. ]]></programlisting>
  646. </example>
  647. <para>
  648. <classname>Zend_Db_Select</classname> automatically puts parentheses around each expression you specify
  649. using the <code>where()</code> or <code>orWhere()</code> methods. This helps to
  650. ensure that Boolean operator precedence does not cause unexpected results.
  651. </para>
  652. <example id="zend.db.select.building.where.example-parens">
  653. <title>Example of parenthesizing Boolean expressions</title>
  654. <programlisting language="php"><![CDATA[
  655. // Build this query:
  656. // SELECT product_id, product_name, price
  657. // FROM "products"
  658. // WHERE (price < 100.00 OR price > 500.00)
  659. // AND (product_name = 'Apple')
  660. $minimumPrice = 100;
  661. $maximumPrice = 500;
  662. $prod = 'Apple';
  663. $select = $db->select()
  664. ->from('products',
  665. array('product_id', 'product_name', 'price'))
  666. ->where("price < $minimumPrice OR price > $maximumPrice")
  667. ->where('product_name = ?', $prod);
  668. ]]></programlisting>
  669. </example>
  670. <para>
  671. In the example above, the results would be quite different without the parentheses,
  672. because <code>AND</code> has higher precedence than <code>OR</code>. <classname>Zend_Db_Select</classname>
  673. applies the parentheses so the effect is that each expression in successive calls
  674. to the <code>where()</code> bind more tightly than the <code>AND</code> that
  675. combines the expressions.
  676. </para>
  677. </sect3>
  678. <sect3 id="zend.db.select.building.group">
  679. <title>Adding a GROUP BY Clause</title>
  680. <para>
  681. In SQL, the <code>GROUP BY</code> clause allows you to reduce the rows of a query
  682. result set to one row per unique value found in the column(s) named in the
  683. <code>GROUP BY</code> clause.
  684. </para>
  685. <para>
  686. In <classname>Zend_Db_Select</classname>, you can specify the column(s) to use for calculating the groups
  687. of rows using the <code>group()</code> method. The argument to this method is a
  688. column or an array of columns to use in the <code>GROUP BY</code> clause.
  689. </para>
  690. <example id="zend.db.select.building.group.example">
  691. <title>Example of the group() method</title>
  692. <programlisting language="php"><![CDATA[
  693. // Build this query:
  694. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  695. // FROM "products" AS p JOIN "line_items" AS l
  696. // ON p.product_id = l.product_id
  697. // GROUP BY p.product_id
  698. $select = $db->select()
  699. ->from(array('p' => 'products'),
  700. array('product_id'))
  701. ->join(array('l' => 'line_items'),
  702. 'p.product_id = l.product_id',
  703. array('line_items_per_product' => 'COUNT(*)'))
  704. ->group('p.product_id');
  705. ]]></programlisting>
  706. </example>
  707. <para>
  708. Like the columns array in the <code>from()</code> method, you can use correlation
  709. names in the column name strings, and the column is quoted as an identifier unless
  710. the string contains parentheses or is an object of type <classname>Zend_Db_Expr</classname>.
  711. </para>
  712. </sect3>
  713. <sect3 id="zend.db.select.building.having">
  714. <title>Adding a HAVING Clause</title>
  715. <para>
  716. In SQL, the <code>HAVING</code> clause applies a restriction condition on groups of
  717. rows. This is similar to how a <code>WHERE</code> clause applies a restriction
  718. condition on rows. But the two clauses are different because <code>WHERE</code>
  719. conditions are applied before groups are defined, whereas <code>HAVING</code>
  720. conditions are applied after groups are defined.
  721. </para>
  722. <para>
  723. In <classname>Zend_Db_Select</classname>, you can specify conditions for restricting groups using the
  724. <code>having()</code> method. Its usage is similar to that of the
  725. <code>where()</code> method. The first argument is a string containing a SQL
  726. expression. The optional second argument is a value that is used to replace a
  727. positional parameter placeholder in the SQL expression. Expressions given in
  728. multiple invocations of the <code>having()</code> method are combined using the
  729. Boolean <code>AND</code> operator, or the <code>OR</code> operator if you use the
  730. <code>orHaving()</code> method.
  731. </para>
  732. <example id="zend.db.select.building.having.example">
  733. <title>Example of the having() method</title>
  734. <programlisting language="php"><![CDATA[
  735. // Build this query:
  736. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  737. // FROM "products" AS p JOIN "line_items" AS l
  738. // ON p.product_id = l.product_id
  739. // GROUP BY p.product_id
  740. // HAVING line_items_per_product > 10
  741. $select = $db->select()
  742. ->from(array('p' => 'products'),
  743. array('product_id'))
  744. ->join(array('l' => 'line_items'),
  745. 'p.product_id = l.product_id',
  746. array('line_items_per_product' => 'COUNT(*)'))
  747. ->group('p.product_id')
  748. ->having('line_items_per_product > 10');
  749. ]]></programlisting>
  750. </example>
  751. <note>
  752. <para>
  753. No quoting is applied to expressions given to the <code>having()</code> or
  754. <code>orHaving()</code> methods. If you have column names that need to be
  755. quoted, you must use <code>quoteIdentifier()</code> as you form the string for
  756. the condition.
  757. </para>
  758. </note>
  759. </sect3>
  760. <sect3 id="zend.db.select.building.order">
  761. <title>Adding an ORDER BY Clause</title>
  762. <para>
  763. In SQL, the <code>ORDER BY</code> clause specifies one or more columns or
  764. expressions by which the result set of a query is sorted. If multiple columns are
  765. listed, the secondary columns are used to resolve ties; the sort order is
  766. determined by the secondary columns if the preceding columns contain identical
  767. values. The default sorting is from least value to greatest value. You can also
  768. sort by greatest value to least value for a given column in the list by specifying
  769. the keyword <code>DESC</code> after that column.
  770. </para>
  771. <para>
  772. In <classname>Zend_Db_Select</classname>, you can use the <code>order()</code> method to specify a column
  773. or an array of columns by which to sort. Each element of the array is a string
  774. naming a column. Optionally with the <code>ASC</code> <code>DESC</code> keyword
  775. following it, separated by a space.
  776. </para>
  777. <para>
  778. Like in the <code>from()</code> and <code>group()</code> methods, column names are
  779. quoted as identifiers, unless they contain contain parentheses or are an object of
  780. type <classname>Zend_Db_Expr</classname>.
  781. </para>
  782. <example id="zend.db.select.building.order.example">
  783. <title>Example of the order() method</title>
  784. <programlisting language="php"><![CDATA[
  785. // Build this query:
  786. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  787. // FROM "products" AS p JOIN "line_items" AS l
  788. // ON p.product_id = l.product_id
  789. // GROUP BY p.product_id
  790. // ORDER BY "line_items_per_product" DESC, "product_id"
  791. $select = $db->select()
  792. ->from(array('p' => 'products'),
  793. array('product_id'))
  794. ->join(array('l' => 'line_items'),
  795. 'p.product_id = l.product_id',
  796. array('line_items_per_product' => 'COUNT(*)'))
  797. ->group('p.product_id')
  798. ->order(array('line_items_per_product DESC',
  799. 'product_id'));
  800. ]]></programlisting>
  801. </example>
  802. </sect3>
  803. <sect3 id="zend.db.select.building.limit">
  804. <title>Adding a LIMIT Clause</title>
  805. <para>
  806. Some RDBMS brands extend SQL with a query clause known as the <code>LIMIT</code>
  807. clause. This clause reduces the number of rows in the result set to at most a
  808. number you specify. You can also specify to skip a number of rows before starting
  809. to output. This feature makes it easy to take a subset of a result set, for example
  810. when displaying query results on progressive pages of output.
  811. </para>
  812. <para>
  813. In <classname>Zend_Db_Select</classname>, you can use the <code>limit()</code> method to specify the count
  814. of rows and the number of rows to skip. The first argument to this method is the
  815. desired count of rows. The second argument is the number of rows to skip.
  816. </para>
  817. <example id="zend.db.select.building.limit.example">
  818. <title>Example of the limit() method</title>
  819. <programlisting language="php"><![CDATA[
  820. // Build this query:
  821. // SELECT p."product_id", p."product_name"
  822. // FROM "products" AS p
  823. // LIMIT 10, 20
  824. $select = $db->select()
  825. ->from(array('p' => 'products'),
  826. array('product_id', 'product_name'))
  827. ->limit(10, 20);
  828. ]]></programlisting>
  829. </example>
  830. <note>
  831. <para>
  832. The <code>LIMIT</code> syntax is not supported by all RDBMS brands. Some RDBMS
  833. require different syntax to support similar functionality. Each
  834. <classname>Zend_Db_Adapter_Abstract</classname> class includes a method to produce SQL appropriate for
  835. that RDBMS.
  836. </para>
  837. </note>
  838. <para>
  839. Use the <code>limitPage()</code> method for an alternative way to specify row count
  840. and offset. This method allows you to limit the result set to one of a series of
  841. fixed-length subsets of rows from the query's total result set. In other words, you
  842. specify the length of a "page" of results, and the ordinal number of the single
  843. page of results you want the query to return. The page number is the first argument
  844. of the <code>limitPage()</code> method, and the page length is the second argument.
  845. Both arguments are required; they have no default values.
  846. </para>
  847. <example id="zend.db.select.building.limit.example2">
  848. <title>Example of the limitPage() method</title>
  849. <programlisting language="php"><![CDATA[
  850. // Build this query:
  851. // SELECT p."product_id", p."product_name"
  852. // FROM "products" AS p
  853. // LIMIT 10, 20
  854. $select = $db->select()
  855. ->from(array('p' => 'products'),
  856. array('product_id', 'product_name'))
  857. ->limitPage(2, 10);
  858. ]]></programlisting>
  859. </example>
  860. </sect3>
  861. <sect3 id="zend.db.select.building.distinct">
  862. <title>Adding the DISTINCT Query Modifier</title>
  863. <para>
  864. The <code>distinct()</code> method enables you to add the <code>DISTINCT</code>
  865. keyword to your SQL query.
  866. </para>
  867. <example id="zend.db.select.building.distinct.example">
  868. <title>Example of the distinct() method</title>
  869. <programlisting language="php"><![CDATA[
  870. // Build this query:
  871. // SELECT DISTINCT p."product_name"
  872. // FROM "products" AS p
  873. $select = $db->select()
  874. ->distinct()
  875. ->from(array('p' => 'products'), 'product_name');
  876. ]]></programlisting>
  877. </example>
  878. </sect3>
  879. <sect3 id="zend.db.select.building.for-update">
  880. <title>Adding the FOR UPDATE Query Modifier</title>
  881. <para>
  882. The <code>forUpdate()</code> method enables you to add the <code>FOR UPDATE</code>
  883. modifier to your SQL query.
  884. </para>
  885. <example id="zend.db.select.building.for-update.example">
  886. <title>Example of forUpdate() method</title>
  887. <programlisting language="php"><![CDATA[
  888. // Build this query:
  889. // SELECT FOR UPDATE p.*
  890. // FROM "products" AS p
  891. $select = $db->select()
  892. ->forUpdate()
  893. ->from(array('p' => 'products'));
  894. ]]></programlisting>
  895. </example>
  896. </sect3>
  897. </sect2>
  898. <sect2 id="zend.db.select.execute">
  899. <title>Executing Select Queries</title>
  900. <para>
  901. This section describes how to execute the query represented by a <classname>Zend_Db_Select</classname> object.
  902. </para>
  903. <sect3 id="zend.db.select.execute.query-adapter">
  904. <title>Executing Select Queries from the Db Adapter</title>
  905. <para>
  906. You can execute the query represented by the <classname>Zend_Db_Select</classname> object by passing it as
  907. the first argument to the <code>query()</code> method of a <classname>Zend_Db_Adapter_Abstract</classname>
  908. object. Use the <classname>Zend_Db_Select</classname> objects instead of a string query.
  909. </para>
  910. <para>
  911. The <code>query()</code> method returns an object of type <classname>Zend_Db_Statement</classname> or
  912. PDOStatement, depending on the adapter type.
  913. </para>
  914. <example id="zend.db.select.execute.query-adapter.example">
  915. <title>Example using the Db adapter's query() method</title>
  916. <programlisting language="php"><![CDATA[
  917. $select = $db->select()
  918. ->from('products');
  919. $stmt = $db->query($select);
  920. $result = $stmt->fetchAll();
  921. ]]></programlisting>
  922. </example>
  923. </sect3>
  924. <sect3 id="zend.db.select.execute.query-select">
  925. <title>Executing Select Queries from the Object</title>
  926. <para>
  927. As an alternative to using the <code>query()</code> method of the adapter object,
  928. you can use the <code>query()</code> method of the <classname>Zend_Db_Select</classname> object. Both
  929. methods return an object of type <classname>Zend_Db_Statement</classname> or PDOStatement, depending on
  930. the adapter type.
  931. </para>
  932. <example id="zend.db.select.execute.query-select.example">
  933. <title>Example using the Select object's query method</title>
  934. <programlisting language="php"><![CDATA[
  935. $select = $db->select()
  936. ->from('products');
  937. $stmt = $select->query();
  938. $result = $stmt->fetchAll();
  939. ]]></programlisting>
  940. </example>
  941. </sect3>
  942. <sect3 id="zend.db.select.execute.tostring">
  943. <title>Converting a Select Object to a SQL String</title>
  944. <para>
  945. If you need access to a string representation of the SQL query corresponding to the
  946. <classname>Zend_Db_Select</classname> object, use the <code>__toString()</code> method.
  947. </para>
  948. <example id="zend.db.select.execute.tostring.example">
  949. <title>Example of the __toString() method</title>
  950. <programlisting language="php"><![CDATA[
  951. $select = $db->select()
  952. ->from('products');
  953. $sql = $select->__toString();
  954. echo "$sql\n";
  955. // The output is the string:
  956. // SELECT * FROM "products"
  957. ]]></programlisting>
  958. </example>
  959. </sect3>
  960. </sect2>
  961. <sect2 id="zend.db.select.other">
  962. <title>Other methods</title>
  963. <para>
  964. This section describes other methods of the <classname>Zend_Db_Select</classname> class that are not covered
  965. above: <code>getPart()</code> and <code>reset()</code>.
  966. </para>
  967. <sect3 id="zend.db.select.other.get-part">
  968. <title>Retrieving Parts of the Select Object</title>
  969. <para>
  970. The <code>getPart()</code> method returns a representation of one part of your SQL
  971. query. For example, you can use this method to return the array of expressions for
  972. the <code>WHERE</code> clause, or the array of columns (or column expressions) that
  973. are in the <code>SELECT</code> list, or the values of the count and offset for the
  974. <code>LIMIT</code> clause.
  975. </para>
  976. <para>
  977. The return value is not a string containing a fragment of SQL syntax. The return
  978. value is an internal representation, which is typically an array structure
  979. containing values and expressions. Each part of the query has a different
  980. structure.
  981. </para>
  982. <para>
  983. The single argument to the <code>getPart()</code> method is a string that
  984. identifies which part of the Select query to return. For example, the string
  985. <code>'from'</code> identifies the part of the Select object that stores
  986. information about the tables in the <code>FROM</code> clause, including joined
  987. tables.
  988. </para>
  989. <para>
  990. The <classname>Zend_Db_Select</classname> class defines constants you can use for parts of the SQL query.
  991. You can use these constant definitions, or you can the literal strings.
  992. </para>
  993. <table id="zend.db.select.other.get-part.table">
  994. <title>Constants used by getPart() and reset()</title>
  995. <tgroup cols="2">
  996. <thead>
  997. <row>
  998. <entry>Constant</entry>
  999. <entry>String value</entry>
  1000. </row>
  1001. </thead>
  1002. <tbody>
  1003. <row>
  1004. <entry><classname>Zend_Db_Select::DISTINCT</classname></entry>
  1005. <entry><code>'distinct'</code></entry>
  1006. </row>
  1007. <row>
  1008. <entry><classname>Zend_Db_Select::FOR_UPDATE</classname></entry>
  1009. <entry><code>'forupdate'</code></entry>
  1010. </row>
  1011. <row>
  1012. <entry><classname>Zend_Db_Select::COLUMNS</classname></entry>
  1013. <entry><code>'columns'</code></entry>
  1014. </row>
  1015. <row>
  1016. <entry><classname>Zend_Db_Select::FROM</classname></entry>
  1017. <entry><code>'from'</code></entry>
  1018. </row>
  1019. <row>
  1020. <entry><classname>Zend_Db_Select::WHERE</classname></entry>
  1021. <entry><code>'where'</code></entry>
  1022. </row>
  1023. <row>
  1024. <entry><classname>Zend_Db_Select::GROUP</classname></entry>
  1025. <entry><code>'group'</code></entry>
  1026. </row>
  1027. <row>
  1028. <entry><classname>Zend_Db_Select::HAVING</classname></entry>
  1029. <entry><code>'having'</code></entry>
  1030. </row>
  1031. <row>
  1032. <entry><classname>Zend_Db_Select::ORDER</classname></entry>
  1033. <entry><code>'order'</code></entry>
  1034. </row>
  1035. <row>
  1036. <entry><classname>Zend_Db_Select::LIMIT_COUNT</classname></entry>
  1037. <entry><code>'limitcount'</code></entry>
  1038. </row>
  1039. <row>
  1040. <entry><classname>Zend_Db_Select::LIMIT_OFFSET</classname></entry>
  1041. <entry><code>'limitoffset'</code></entry>
  1042. </row>
  1043. </tbody>
  1044. </tgroup>
  1045. </table>
  1046. <example id="zend.db.select.other.get-part.example">
  1047. <title>Example of the getPart() method</title>
  1048. <programlisting language="php"><![CDATA[
  1049. $select = $db->select()
  1050. ->from('products')
  1051. ->order('product_id');
  1052. // You can use a string literal to specify the part
  1053. $orderData = $select->getPart( 'order' );
  1054. // You can use a constant to specify the same part
  1055. $orderData = $select->getPart( Zend_Db_Select::ORDER );
  1056. // The return value may be an array structure, not a string.
  1057. // Each part has a different structure.
  1058. print_r( $orderData );
  1059. ]]></programlisting>
  1060. </example>
  1061. </sect3>
  1062. <sect3 id="zend.db.select.other.reset">
  1063. <title>Resetting Parts of the Select Object</title>
  1064. <para>
  1065. The <code>reset()</code> method enables you to clear one specified part of the SQL
  1066. query, or else clear all parts of the SQL query if you omit the argument.
  1067. </para>
  1068. <para>
  1069. The single argument is optional. You can specify the part of the query to clear,
  1070. using the same strings you used in the argument to the <code>getPart()</code>
  1071. method. The part of the query you specify is reset to a default state.
  1072. </para>
  1073. <para>
  1074. If you omit the parameter, <code>reset()</code> changes all parts of the query to
  1075. their default state. This makes the <classname>Zend_Db_Select</classname> object equivalent to a new
  1076. object, as though you had just instantiated it.
  1077. </para>
  1078. <example id="zend.db.select.other.reset.example">
  1079. <title>Example of the reset() method</title>
  1080. <programlisting language="php"><![CDATA[
  1081. // Build this query:
  1082. // SELECT p.*
  1083. // FROM "products" AS p
  1084. // ORDER BY "product_name"
  1085. $select = $db->select()
  1086. ->from(array('p' => 'products')
  1087. ->order('product_name');
  1088. // Changed requirement, instead order by a different columns:
  1089. // SELECT p.*
  1090. // FROM "products" AS p
  1091. // ORDER BY "product_id"
  1092. // Clear one part so we can redefine it
  1093. $select->reset( Zend_Db_Select::ORDER );
  1094. // And specify a different column
  1095. $select->order('product_id');
  1096. // Clear all parts of the query
  1097. $select->reset();
  1098. ]]></programlisting>
  1099. </example>
  1100. </sect3>
  1101. </sect2>
  1102. </sect1>
  1103. <!--
  1104. vim:se ts=4 sw=4 et:
  1105. -->