其他条件
在DAO
或者选择器
里都可以调用条件方法,方法可传递式调用,相同方法内的条件会自动合并
其中包括group
,addition
,order
,limit
,having
// SELECT `user`.`id`, avg(`user`.`cash`) AS 'a_c' FROM `TABLE` `user` WHERE ... GROUP BY `user`.`id`,`user`.`type` HAVING `a_c` >= 1000 ORDER BY `a_c` DESC, `id` ASC LIMIT 20,10; $this ->userDAO //->filter(...) - ->
addition (array ('avg' =>array ('cash' =>'a_c' ))- ->
group (array ('id' ,'type' ))- ->
having (array ('>=' =>array ('a_c' => 1000)))- ->
order (array ('a_c' =>'DESC' ,'id' =>'ASC' ))// limit 第一个参数为取的条数,第二个参数为起始位置(默认为0) - ->
limit (10, 20)- ->
query (array ('id' ));
Biny v2.9.2之后支持自定义排序(order by field),随机排序(order by rand)逻辑,同时也支持了order方法多次调用
// ... ORDER BY `type` asc,FIELD(`status`,'1','9','3','6'),RAND() $this ->userDAO ->order (['type' =>'asc' ,'status' =>[1,9,3,6]])- ->
order ('rand' )->query ();
addition
是对数据做计算处理的方法,提供了max
,count
,sum
,min
,avg
等计算方法
多联表时同样需要用到二维数组
// SELECT avg(`user`.`cash`) AS 'a_c', avg(`user`.`time`) AS 'time', sum(`user`.`cash`) AS 'total', min(`test`.`testid`) AS 'testid' FROM `TABLE1` `user` join `TABLE2` `test` ON `user`.`id` = `test`.`user_id` WHERE ... GROUP BY `user`.`id`,`user`.`type` HAVING `a_c` >= 1000 ORDER BY `a_c` DESC, `id` ASC LIMIT 0,10; $DAO =$this ->userDAO ->join ($this ->testDAO ,array ('id' =>'user_id' ))$DAO //->filter(...) - ->
addition (array (array ('avg' =>array ('cash' =>'a_c' ,'time' ),'sum' =>array ('cash' =>'total' ),- ),
array ('min' =>array ('testid' ),- ),
- )->
query ();
每次添加条件后都是独立的,不会影响
原DAO 或者 选择器,可以放心的使用
// 这个对象不会因添加条件而变化 $filter =$this ->userDAO ->filter (array ('id' =>array (1,2,3,'test' )));// 2 $count =$filter ->limit (2)->count ()// 4 $count =$filter ->count ()// 100 (user表总行数) $count =$this ->userDAO ->count ()
Biny同时也可以使用Database::field()
来支持复杂的Group By
语句,例如:
// SELECT FROM_UNIXTIME(time,'%Y-%m-%d') AS time, count(*) AS 'count' FROM `user` Group By FROM_UNIXTIME(time,'%Y-%m-%d') $result =$this ->userDAO ->group (Database::field ("FROM_UNIXTIME(time,'%Y-%m-%d')" ))- ->
addition (array ('count' =>'*' ))- ->
query ("FROM_UNIXTIME(time,'%Y-%m-%d') AS time");