ThinkPHP中的查询语言
一、普通查询
在查询带入where条件等,最少有三种形式
1、字符串形式
'id>5 and id<9'
2、数组形式
- $user=M('user');
- $data['username']='liwenkai';
- $list=$user->where(array('username'=>'liwenkai'))->select();
- $list=$user->where($data)->select();
3、对象形式
- $user=M('user');
- $a=new stdClass();
- $a->username='liwenkai';
- $list=$user->where($a)->select();
4、查询表达式
EQ 等于
NEQ 不等于
GT 大于
EGT 大于等于
LT 小于
ELT 小于等于
LIKE 等价与sql中的like
[NOT] BETWEEN 查询区间
[NOT] IN 查询集合
EXP 指使用标准SQL语句,实现更加复杂的情况
- $data['字段名']=array('是表达式','查询条件');
- $data['liwenkai']='liwenkai';实际上是指$data['liwenkai']=array('eq','liwenkai');
-
- $data['username']=array('like','peng%');
- $list=$user->where($data)->select();
二、区间查询
- $user=M('user');
- $data['id']=array(array('gt',20),array('lt',23),'and');
- $list=$user->where($data)->select();
- dump($list);
- $data['username']=array(array('like','p%'),array('like','h%'),'or');
三、组合查询
- $user=M('user');
- $data['username']='pengyanjie';
- $data['password']=array('eq','pengyanjie');
- $data['id']=array('lt',30);
- $data['_logic']='or';
- $list=$user->where($data)->select();
- dump($list);
四、复合查询
- $user=M('user');
- $data['username']=array('eq','pengyanjie');
- $data['password']=array('like','p%');
- $data['_logic']='or';
- $where['_complex']=$where;
- $where['id']=array('lt',30);
- $list=$user->where($data)->select();
- dump($list);
-
五、统计查询
- echo $user->count();
- echo '<br>';
- echo $user->max('id');
- echo '<br>';
- echo $user->where('id<30')->min('id');
- echo '<br>';
- echo $user->avg('id');
- echo '<br>';
- echo $user->sum('id');
六、定位查询
- $user=new AdvModel('user');
-
- $list=$user->order('id desc')->getN(2);
- dump($list);
-
- $list=$user->order('id desc')->last();
- $list=$user->order('id desc')->first();
七、SQL查询
excute()主要用于更新和写入
- $Model = new Model()
-
- $Model->execute( "update think_user set name='thinkPHP' where status=1" );
query()主要用于查询
- $user=M();
- $list=$user->query('select * from aoli_user order by id desc');
- dump($list);
八、动态查询
- $user=M('user');
- $list=$user->getByusername('pengyanjie');
- $list=$user->getByusername('pengyanjie');
- dump($list);
- $user=new AdvModel('user');
- $list=$user->top5();
- dump($list);
|