Yii学习(九)数据库 Query Builder 上
2012-09-19 14:41
176 查看
1.建立数据库连接
2.示范:
3.连贯操作
Just beginning!
1.准备
2.方法
select():
selectDistinct():
from():
where():
order():
limit() and offset():
join() and its variants:
group():
having():
union():
3.执行
返回sql语句
等效
绑定
清理上一次操作
insert():
update():
delete():
$connection=new CDbConnection($dsn,$username,$password); // 建立连接。你可以使用 try...catch 捕获可能抛出的异常 $connection->active=true; ...... $connection->active=false; // 关闭连接
2.示范:
$user = Yii::app()->db->createCommand() ->select('id, username, profile') ->from('tbl_user u') ->join('tbl_profile p', 'u.id=p.user_id') ->where('id=:id', array(':id'=>$id)) ->queryRow();
3.连贯操作
$command = Yii::app()->db->createCommand('SELECT * FROM tbl_user'); // the following line will NOT append WHERE clause to the above SQL $command->where('id=:id', array(':id'=>$id));
Just beginning!
1.准备
$command = Yii::app()->db->createCommand();
2.方法
select(): specifies the SELECT part of the query selectDistinct(): specifies the SELECT part of the query and turns on the DISTINCT flag from(): specifies the FROM part of the query where(): specifies the WHERE part of the query join(): appends an inner join query fragment leftJoin(): appends a left outer join query fragment rightJoin(): appends a right outer join query fragment crossJoin(): appends a cross join query fragment naturalJoin(): appends a natural join query fragment group(): specifies the GROUP BY part of the query having(): specifies the HAVING part of the query order(): specifies the ORDER BY part of the query limit(): specifies the LIMIT part of the query offset(): specifies the OFFSET part of the query union(): appends a UNION query fragment
select():
function select($columns='*') // SELECT * select() // SELECT `id`, `username` select('id, username') // SELECT `tbl_user`.`id`, `username` AS `name` select('tbl_user.id, username as name') // SELECT `id`, `username` select(array('id', 'username')) // SELECT `id`, count(*) as num select(array('id', 'count(*) as num'))
selectDistinct():
function selectDistinct($columns) selectDistinct('id, username') => SELECT DISTINCT `id`, `username`
from():
function from($tables) // FROM `tbl_user` from('tbl_user') // FROM `tbl_user` `u`, `public`.`tbl_profile` `p` from('tbl_user u, public.tbl_profile p') // FROM `tbl_user`, `tbl_profile` from(array('tbl_user', 'tbl_profile')) // FROM `tbl_user`, (select * from tbl_profile) p from(array('tbl_user', '(select * from tbl_profile) p'))
where():
function where($conditions, $params=array()) array(operator, operand1, operand2, ...) // WHERE id=1 or id=2 where('id=1 or id=2') // WHERE id=:id1 or id=:id2 where('id=:id1 or id=:id2', array(':id1'=>1, ':id2'=>2)) // WHERE id=1 OR id=2 where(array('or', 'id=1', 'id=2')) // WHERE id=1 AND (type=2 OR type=3) where(array('and', 'id=1', array('or', 'type=2', 'type=3'))) // WHERE `id` IN (1, 2) where(array('in', 'id', array(1, 2)) // WHERE `id` NOT IN (1, 2) where(array('not in', 'id', array(1,2))) // WHERE `name` LIKE '%Qiang%' where(array('like', 'name', '%Qiang%')) // WHERE `name` LIKE '%Qiang' AND `name` LIKE '%Xue' where(array('like', 'name', array('%Qiang', '%Xue'))) // WHERE `name` LIKE '%Qiang' OR `name` LIKE '%Xue' where(array('or like', 'name', array('%Qiang', '%Xue'))) // WHERE `name` NOT LIKE '%Qiang%' where(array('not like', 'name', '%Qiang%')) // WHERE `name` NOT LIKE '%Qiang%' OR `name` NOT LIKE '%Xue%' where(array('or not like', 'name', array('%Qiang%', '%Xue%'))) $keyword=$_GET['q']; // escape % and _ characters $keyword=strtr($keyword, array('%'=>'\%', '_'=>'\_')); $command->where(array('like', 'title', '%'.$keyword.'%'));
order():
function order($columns) // ORDER BY `name`, `id` DESC order('name, id desc') // ORDER BY `tbl_profile`.`name`, `id` DESC order(array('tbl_profile.name', 'id desc'))
limit() and offset():
function limit($limit, $offset=null) function offset($offset) // LIMIT 10 limit(10) // LIMIT 10 OFFSET 20 limit(10, 20) // OFFSET 20 offset(20)
join() and its variants:
function join($table, $conditions, $params=array()) function leftJoin($table, $conditions, $params=array()) function rightJoin($table, $conditions, $params=array()) function crossJoin($table) function naturalJoin($table) // JOIN `tbl_profile` ON user_id=id join('tbl_profile', 'user_id=id') // LEFT JOIN `pub`.`tbl_profile` `p` ON p.user_id=id AND type=1 leftJoin('pub.tbl_profile p', 'p.user_id=id AND type=:type', array(':type'=>1))
group():
function group($columns) // GROUP BY `name`, `id` group('name, id') // GROUP BY `tbl_profile`.`name`, `id` group(array('tbl_profile.name', 'id'))
having():
function having($conditions, $params=array()) // HAVING id=1 or id=2 having('id=1 or id=2') // HAVING id=1 OR id=2 having(array('or', 'id=1', 'id=2'))
union():
function union($sql) // UNION (select * from tbl_profile) union('select * from tbl_profile')
3.执行
$users = Yii::app()->db->createCommand() ->select('*') ->from('tbl_user') ->queryAll();
返回sql语句
$sql = Yii::app()->db->createCommand() ->select('*') ->from('tbl_user') ->text;
等效
$command->select(array('id', 'username')); $command->select = array('id', 'username');
绑定
$row = Yii::app()->db->createCommand(array( 'select' => array('id', 'username'), 'from' => 'tbl_user', 'where' => 'id=:id', 'params' => array(':id'=>1), ))->queryRow();
清理上一次操作
$command = Yii::app()->db->createCommand(); $users = $command->select('*')->from('tbl_users')->queryAll(); $command->reset(); // clean up the previous query $posts = $command->select('*')->from('tbl_posts')->queryAll();
insert():
function insert($table, $columns) // build and execute the following SQL: // INSERT INTO `tbl_user` (`name`, `email`) VALUES (:name, :email) $command->insert('tbl_user', array( 'name'=>'Tester', 'email'=>'tester@example.com', ));
update():
function update($table, $columns, $conditions='', $params=array()) // build and execute the following SQL: // UPDATE `tbl_user` SET `name`=:name WHERE id=:id $command->update('tbl_user', array( 'name'=>'Tester', ), 'id=:id', array(':id'=>1));
delete():
function delete($table, $conditions='', $params=array()) // build and execute the following SQL: // DELETE FROM `tbl_user` WHERE id=:id $command->delete('tbl_user', 'id=:id', array(':id'=>1));
相关文章推荐
- YII Framework学习教程-YII的Model-数据库操作4-Query Builder使用举例
- Yii学习(十)数据库 Query Builder 下
- yii框架学习经历-6.4数据库迁移
- YII Framework学习教程-YII的Model-数据库操作1-CActiveRecord
- Yii学习笔记-数据库访问
- YII Framework学习教程-YII的Model-数据库操作1-CActiveRecord
- Yii的学习(3)--查询生成器 (Query Builder)
- Laravel学习笔记(五)---操作数据库--查询构建器(Query Builder)
- YII Framework学习教程-YII的Model-数据库操作2-Relational Active Record翻译补全
- 关于后盾网yii框架的学习小结(6)--数据库连接配置与模型定义与后台登陆验
- YII学习,实现基于数据库的后台登录,用户体系.
- yii 学习笔记值 如何批量像数据库插入数据
- <yii 框架学习> <转> 关于yii数据库添加新字段之后model类的修改
- YII Framework学习教程-YII的Model-数据库操作2-多关系的CActiveRecord使用举例
- yii 学习笔记七、数据库连接配置
- YII Framework学习教程-YII的Model-数据库操作3-自定义的DAO操作
- [我的PHP之旅] YII框架学习 03.连接数据库(使用ActiveRecord)
- YII Framework学习教程-YII的Model-数据库操作4-<Query Builder>翻译
- 关于我学习Yii框架 - 数据库relation的使用
- Yii框架官方指南系列24——使用数据库:Query Builder