zend 2.2 db select 使用例子
<?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 使用例子的更多相关文章
- laravel 中将DB::select 得到的内容转为数组
$sql = "select count(*) as num from api_log where uid='{$this->uid}'"; $ ...
- 一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子
一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子 最近,由于云计算的发展,数据库技术也从结构式数据库发展到NoSQL数据库,存储模式从结构化的关系存储到现在如火如荼的key/val ...
- web.py 使用 db.select 返回的数据只能遍历一次
2013-10-05 23:04:33| 1. web.py 使用 db.select 返回的数据只能遍历一次import webdb = web.database(dbn='mysql', db ...
- [Spark][Python]DataFrame select 操作例子
[Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...
- [Spark][Python]DataFrame select 操作例子II
[Spark][Python]DataFrame中取出有限个记录的 继续 In [4]: peopleDF.select("age","name") In ...
- python使用 db.select 返回的数据只能遍历一次
python中通过find从mongo中查出的数据,或者通过select返回的数据,其实返回的是游标,当你进行便利一次之后,游标指向最后, 所以当你再一次进行便利时,便出现数据为空的现象. 解决办法: ...
- 转 linux socket的select函数例子
使用select函数可以以非阻塞的方式和多个socket通信.程序只是演示select函数的使用,功能非常简单,即使某个连接关闭以后也不会修改当前连接数,连接数达到最大值后会终止程序. 1. 程序使用 ...
- web.py simpletodo 例子
一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...
- Zend Framework 留言本实战(转)
一.环境搭建和ZF安装 *[注]本节内容大部分来至Zend Framework官方手册 1.1 Zend Framework下载 Zend Framework 使 ...
随机推荐
- java程序运行一段时间之后停止
原创文章,未经作者允许,禁止转载!!!!!!! 如何用java是一段代码运行一段时间之后自动停止运行? 就拿打印随机函数的代码来做例子吧,让程序随机打印1-10的数字,打印十秒钟后停止打印: publ ...
- HTML5游戏开发系列教程9(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-9/ 今天我们将继续使用canvas来进行HTML5游戏开发系列的 ...
- Scrapy:学习笔记(1)——XPath
Scrapy:学习笔记(1)——XPath 1.快速开始 XPath是一种可以快速在HTML文档中选择并抽取元素.属性和文本的方法. 在Chrome,打开开发者工具,可以使用$x工具函数来使用XPat ...
- 全新办公方式,iClap引领企业级服务新浪潮
随着企业级服务,SaaS市场的飞速发展,国内市场上,伴随着“马云又一个反人类的社交梦-钉钉”的出现与强势推广,企业协同办公类的产品被不断的呈现在企业的视线中,一时间,似乎我们传统的工作方式好像已经成了 ...
- echarts 不同区域背景色不同 废了我一天的时间
var result = echarts.init(document.getElementById('result')); var option1 = { title: { text: '设备阶段故障 ...
- Java StringBuffer 和 StringBuilder 类
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够 ...
- mongo数据库连接工具类(C#)
Framework版本:.Net Framework 4 using System; using System.Collections.Generic; using System.Linq; usin ...
- 常用php操作redis命令整理(五)ZSET类型
ZADD 向有序集合插入一个元素,元素关联一个数值,插入成功返回1,同时集合元素不可以重复, 如果元素已经存在返回 0 <?php var_dump($redis->zadd(,'A')) ...
- 微信小程序:工具配置 project.config.json
微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...
- Hash课堂测试补写
Hash课堂测试补写 测试要求: 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,6 ...