<span <?php<br /><br />use</span><span Zend\Db\Sql\Select; </span><span //</span><span basic table</span> <span $select0</span> = <span new</span><span Select; </span><span $select0</span>->from('foo'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo"'; // table as TableIdentifier</span> <span $select1</span> = <span new</span><span Select; </span><span $select1</span>->from(<span new</span> TableIdentifier('foo', 'bar'<span )); </span><span //</span><span 'SELECT "bar"."foo".* FROM "bar"."foo"'; // table with alias</span> <span $select2</span> = <span new</span><span Select; </span><span $select2</span>->from(<span array</span>('f' => 'foo'<span )); </span><span //</span><span 'SELECT "f".* FROM "foo" AS "f"'; // table with alias with table as TableIdentifier</span> <span $select3</span> = <span new</span><span Select; </span><span $select3</span>->from(<span array</span>('f' => <span new</span> TableIdentifier('foo'<span ))); </span><span //</span><span 'SELECT "f".* FROM "foo" AS "f"'; // columns</span> <span $select4</span> = <span new</span><span Select; </span><span $select4</span>->from('foo')->columns(<span array</span>('bar', 'baz'<span )); </span><span //</span><span 'SELECT "foo"."bar" AS "bar", "foo"."baz" AS "baz" FROM "foo"'; // columns with AS associative array</span> <span $select5</span> = <span new</span><span Select; </span><span $select5</span>->from('foo')->columns(<span array</span>('bar' => 'baz'<span )); </span><span //</span><span 'SELECT "foo"."baz" AS "bar" FROM "foo"'; // columns with AS associative array mixed</span> <span $select6</span> = <span new</span><span Select; </span><span $select6</span>->from('foo')->columns(<span array</span>('bar' => 'baz', 'bam'<span )); </span><span //</span><span 'SELECT "foo"."baz" AS "bar", "foo"."bam" AS "bam" FROM "foo"'; // columns where value is Expression, with AS</span> <span $select7</span> = <span new</span><span Select; </span><span $select7</span>->from('foo')->columns(<span array</span>('bar' => <span new</span> Expression('COUNT(some_column)'<span ))); </span><span //</span><span 'SELECT COUNT(some_column) AS "bar" FROM "foo"'; // columns where value is Expression</span> <span $select8</span> = <span new</span><span Select; </span><span $select8</span>->from('foo')->columns(<span array</span>(<span new</span> Expression('COUNT(some_column) AS bar'<span ))); </span><span //</span><span 'SELECT COUNT(some_column) AS bar FROM "foo"'; // columns where value is Expression with parameters</span> <span $select9</span> = <span new</span><span Select; </span><span $select9</span>->from('foo')-><span columns( </span><span array</span><span ( </span><span new</span><span Expression( </span>'(COUNT(?) + ?) AS ?', <span array</span>('some_column', 5, 'bar'), <span array</span>(Expression::TYPE_IDENTIFIER, Expression::TYPE_VALUE, Expression::<span TYPE_IDENTIFIER) ) ) ); </span><span //</span><span 'SELECT (COUNT("some_column") + ?) AS "bar" FROM "foo"'; // array('column1' => 5); // // 'SELECT (COUNT("some_column") + \'5\') AS "bar" FROM "foo"'; // joins (plain)</span> <span $select10</span> = <span new</span><span Select; </span><span $select10</span>->from('foo')-><span join</span>('zac', 'm = n'<span ); </span><span //</span><span 'SELECT "foo".*, "zac".* FROM "foo" INNER JOIN "zac" ON "m" = "n"'; // join with columns</span> <span $select11</span> = <span new</span><span Select; </span><span $select11</span>->from('foo')-><span join</span>('zac', 'm = n', <span array</span>('bar', 'baz'<span )); </span><span //</span><span 'SELECT "foo".*, "zac"."bar" AS "bar", "zac"."baz" AS "baz" FROM "foo" INNER JOIN "zac" ON "m" = "n"'; // join with alternate type</span> <span $select12</span> = <span new</span><span Select; </span><span $select12</span>->from('foo')-><span join</span>('zac', 'm = n', <span array</span>('bar', 'baz'), Select::<span JOIN_OUTER); </span><span //</span><span 'SELECT "foo".*, "zac"."bar" AS "bar", "zac"."baz" AS "baz" FROM "foo" OUTER JOIN "zac" ON "m" = "n"'; // join with column aliases</span> <span $select13</span> = <span new</span><span Select; </span><span $select13</span>->from('foo')-><span join</span>('zac', 'm = n', <span array</span>('BAR' => 'bar', 'BAZ' => 'baz'<span )); </span><span //</span><span 'SELECT "foo".*, "zac"."bar" AS "BAR", "zac"."baz" AS "BAZ" FROM "foo" INNER JOIN "zac" ON "m" = "n"'; // join with table aliases</span> <span $select14</span> = <span new</span><span Select; </span><span $select14</span>->from('foo')-><span join</span>(<span array</span>('b' => 'bar'), 'b.foo_id = foo.foo_id'<span ); </span><span //</span><span 'SELECT "foo".*, "b".* FROM "foo" INNER JOIN "bar" AS "b" ON "b"."foo_id" = "foo"."foo_id"'; // where (simple string)</span> <span $select15</span> = <span new</span><span Select; </span><span $select15</span>->from('foo')->where('x = 5'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" WHERE x = 5'; // where (returning parameters)</span> <span $select16</span> = <span new</span><span Select; </span><span $select16</span>->from('foo')->where(<span array</span>('x = ?' => 5<span )); </span><span //</span><span 'SELECT "foo".* FROM "foo" WHERE x = ?'; // array('where1' => 5); // // 'SELECT "foo".* FROM "foo" WHERE x = \'5\''; // group</span> <span $select17</span> = <span new</span><span Select; </span><span $select17</span>->from('foo')->group(<span array</span>('col1', 'col2'<span )); </span><span //</span><span 'SELECT "foo".* FROM "foo" GROUP BY "col1", "col2"';</span> <span $select18</span> = <span new</span><span Select; </span><span $select18</span>->from('foo')->group('col1')->group('col2'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" GROUP BY "col1", "col2"';</span> <span $select19</span> = <span new</span><span Select; </span><span $select19</span>->from('foo')->group(<span new</span> Expression('DAY(?)', <span array</span>('col1'), <span array</span>(Expression::<span TYPE_IDENTIFIER))); </span><span //</span><span 'SELECT "foo".* FROM "foo" GROUP BY DAY("col1")'; // having (simple string)</span> <span $select20</span> = <span new</span><span Select; </span><span $select20</span>->from('foo')->having('x = 5'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" HAVING x = 5'; // having (returning parameters)</span> <span $select21</span> = <span new</span><span Select; </span><span $select21</span>->from('foo')->having(<span array</span>('x = ?' => 5<span )); </span><span //</span><span 'SELECT "foo".* FROM "foo" HAVING x = ?'; // array('having1' => 5); // // 'SELECT "foo".* FROM "foo" HAVING x = \'5\''; // order</span> <span $select22</span> = <span new</span><span Select; </span><span $select22</span>->from('foo')->order('c1'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" ORDER BY "c1" ASC'; // multiple order parts</span> <span $select23</span> = <span new</span><span Select; </span><span $select23</span>->from('foo')->order(<span array</span>('c1', 'c2'<span )); </span><span //</span><span 'SELECT "foo".* FROM "foo" ORDER BY "c1" ASC, "c2" ASC'; // mulitple order parts</span> <span $select24</span> = <span new</span><span Select; </span><span $select24</span>->from('foo')->order(<span array</span>('c1' => 'DESC', 'c2' => 'Asc')); <span //</span><span notice partially lower case ASC // 'SELECT "foo".* FROM "foo" ORDER BY "c1" DESC, "c2" ASC';</span> <span $select25</span> = <span new</span><span Select; </span><span $select25</span>->from('foo')->order(<span array</span>('c1' => 'asc'))->order('c2 desc'); <span //</span><span notice partially lower case ASC // 'SELECT "foo".* FROM "foo" ORDER BY "c1" ASC, "c2" DESC'; // limit</span> <span $select26</span> = <span new</span><span Select; </span><span $select26</span>->from('foo')->limit(5<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" LIMIT ?'; // array('limit' => 5); // // 'SELECT "foo".* FROM "foo" LIMIT \'5\''; // limit with offset</span> <span $select27</span> = <span new</span><span Select; </span><span $select27</span>->from('foo')->limit(5)->offset(10<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" LIMIT ? OFFSET ?'; // array('limit' => 5, 'offset' => 10); // // 'SELECT "foo".* FROM "foo" LIMIT \'5\' OFFSET \'10\''; // joins with a few keywords in the on clause</span> <span $select28</span> = <span new</span><span Select; </span><span $select28</span>->from('foo')-><span join</span>('zac', '(m = n AND c.x) BETWEEN x AND y.z'<span ); </span><span //</span><span 'SELECT "foo".*, "zac".* FROM "foo" INNER JOIN "zac" ON ("m" = "n" AND "c"."x") BETWEEN "x" AND "y"."z"'; // order with compound name</span> <span $select29</span> = <span new</span><span Select; </span><span $select29</span>->from('foo')->order('c1.d2'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" ORDER BY "c1"."d2" ASC'; // group with compound name</span> <span $select30</span> = <span new</span><span Select; </span><span $select30</span>->from('foo')->group('c1.d2'<span ); </span><span //</span><span 'SELECT "foo".* FROM "foo" GROUP BY "c1"."d2"'; // join with expression in ON part</span> <span $select31</span> = <span new</span><span Select; </span><span $select31</span>->from('foo')-><span join</span>('zac', <span new</span> Expression('(m = n AND c.x) BETWEEN x AND y.z'<span )); </span><span //</span><span 'SELECT "foo".*, "zac".* FROM "foo" INNER JOIN "zac" ON (m = n AND c.x) BETWEEN x AND y.z'; // subselects</span> <span $select32subselect</span> = <span new</span><span Select; </span><span $select32subselect</span>->from('bar')->where->like('y', '%Foo%'<span ); </span><span $select32</span> = <span new</span><span Select; </span><span $select32</span>->from(<span array</span>('x' => <span $select32subselect</span><span )); </span><span //</span><span 'SELECT "x".* FROM (SELECT "bar".* FROM "bar" WHERE "y" LIKE ?) AS "x"'; // 'SELECT "x".* FROM (SELECT "bar".* FROM "bar" WHERE "y" LIKE \'%Foo%\') AS "x"'; // use array in where, predicate in where</span> <span $select33</span> = <span new</span><span Select; </span><span $select33</span>->from('table')->columns(<span array</span>('*'))->where(<span array</span><span ( </span>'c1' => <span null</span>, 'c2' => <span array</span>(1, 2, 3), <span new</span> \Zend\Db\Sql\Predicate\IsNotNull('c3'<span ) )); </span><span //</span><span 'SELECT "table".* FROM "table" WHERE "c1" IS NULL AND "c2" IN (?, ?, ?) AND "c3" IS NOT NULL'; // 'SELECT "table".* FROM "table" WHERE "c1" IS NULL AND "c2" IN (\'1\', \'2\', \'3\') AND "c3" IS NOT NULL'; // Expression objects in order</span> <span $select34</span> = <span new</span><span Select; </span><span $select34</span>->from('table')->order(<span array</span><span ( </span><span new</span> Expression('isnull(?) DESC', <span array</span>('name'), <span array</span>(Expression::TYPE_IDENTIFIER)), 'name'<span )); </span><span //</span><span 'SELECT "table".* FROM "table" ORDER BY isnull("name") DESC, "name" ASC'; // join with Expression object in COLUMNS part (ZF2-514)</span> <span $select35</span> = <span new</span><span Select; </span><span $select35</span>->from('foo')->columns(<span array</span>())-><span join</span>('bar', 'm = n', <span array</span>('thecount' => <span new</span> Expression("COUNT(*)"<span ))); </span><span //</span><span 'SELECT COUNT(*) AS "thecount" FROM "foo" INNER JOIN "bar" ON "m" = "n"'; // multiple joins with expressions</span> <span $select36</span> = <span new</span><span Select; </span><span $select36</span>->from('foo'<span ) </span>-><span join</span>('tableA', <span new</span> Predicate\Operator('id', '=', 1<span )) </span>-><span join</span>('tableB', <span new</span> Predicate\Operator('id', '=', 2<span )) </span>-><span join</span>('tableC', <span new</span> Predicate\PredicateSet(<span array</span><span ( </span><span new</span> Predicate\Operator('id', '=', 3), <span new</span> Predicate\Operator('number', '>', 20<span ) ))); </span><span //</span><span 'SELECT "foo".*, "tableA".*, "tableB".*, "tableC".* FROM "foo" ' // 'INNER JOIN "tableA" ON "id" = :join1part1 INNER JOIN "tableB" ON "id" = :join2part1 ' // 'INNER JOIN "tableC" ON "id" = :join3part1 AND "number" > :join3part2'; // // 'SELECT "foo".*, "tableA".*, "tableB".*, "tableC".* FROM "foo" ' // 'INNER JOIN "tableA" ON "id" = \'1\' INNER JOIN "tableB" ON "id" = \'2\' ' // 'INNER JOIN "tableC" ON "id" = \'3\' AND "number" > \'20\'';</span>