<?php

use Zend\Db\Sql\Select;

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

zend 2.2 db select 使用例子的更多相关文章

  1. laravel 中将DB::select 得到的内容转为数组

    $sql = "select count(*) as num from api_log where uid='{$this->uid}'";                $ ...

  2. 一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子

    一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子 最近,由于云计算的发展,数据库技术也从结构式数据库发展到NoSQL数据库,存储模式从结构化的关系存储到现在如火如荼的key/val ...

  3. web.py 使用 db.select 返回的数据只能遍历一次

    2013-10-05 23:04:33|   1. web.py 使用 db.select 返回的数据只能遍历一次import webdb = web.database(dbn='mysql', db ...

  4. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

  5. [Spark][Python]DataFrame select 操作例子II

    [Spark][Python]DataFrame中取出有限个记录的   继续 In [4]: peopleDF.select("age","name") In ...

  6. python使用 db.select 返回的数据只能遍历一次

    python中通过find从mongo中查出的数据,或者通过select返回的数据,其实返回的是游标,当你进行便利一次之后,游标指向最后, 所以当你再一次进行便利时,便出现数据为空的现象. 解决办法: ...

  7. 转 linux socket的select函数例子

    使用select函数可以以非阻塞的方式和多个socket通信.程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序. 1. 程序使用 ...

  8. web.py simpletodo 例子

    一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...

  9. Zend Framework 留言本实战(转)

    一.环境搭建和ZF安装              *[注]本节内容大部分来至Zend Framework官方手册       1.1 Zend Framework下载 Zend Framework 使 ...

随机推荐

  1. java基础知识面试题(41-95)

    41.日期和时间:- 如何取得年月日.小时分钟秒?- 如何取得从1970年1月1日0时0分0秒到现在的毫秒数?- 如何取得某月的最后一天?- 如何格式化日期?答:问题1:创建java.util.Cal ...

  2. 如何把PDF文件转换为JPG图片

    大家有遇到需要将PDF文件转换为JPG的情况么,文档资料里面经常会有一些图片类型的内容,有的时候我们需要获取这些图片内容,但是PDF文件又是不可编辑的,那如何把PDF文件转换为JPG图片呢,跟着小编一 ...

  3. web前端几个小知识点笔记

    1.css实现宽度是百分比的盒子为正方形 <div style="width:50%;padding-bottom:50%;height:0px;background:#ccc;&qu ...

  4. cocos代码研究(10)ActionEase子类学习笔记

    理论部分 缓动动作的基类,继承自 ActionInterval类.ActionEase本身是一个抽象的概念父类,开发者最好不要在代码中直接创建它的对象,因为它没有具体的执行效果,这一类的子类速度变化大 ...

  5. Educational Codeforces Round 56 Solution

    A. Dice Rolling 签到. #include <bits/stdc++.h> using namespace std; int t, n; int main() { scanf ...

  6. 2016ACM/ICPC亚洲区沈阳站 Solution

    A - Thickest Burger 水. #include <bits/stdc++.h> using namespace std; int t; int a, b; int main ...

  7. Linux 查看系统所有用户

    grep bash /etc/passwd Linux 查看系统所有用户

  8. Python安装安装.whl包(安装pylint)

    Python安装安装.whl包(安装pylint) Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. 1,,下载.whl ...

  9. GreenOpenPaint的实现(二)打开显示图片

    1.DOC中添加,核心图片文件保存在这里.之所以不用Mat,是因为CVVImage有更好的输入输出函数. 我这里直接使用了public public: CvvImage m_image; 2.重载打开 ...

  10. struts1和struts2比较