php框架

 首页 > php框架 > ThinkPHP > thinkphp中的查询技巧总结

thinkphp中的查询技巧总结

分享到:
【字体:
导读:
          最近刚刚开始学习 thinkphp这套框架,对于thinkphp的强大真的是让哥叹为观止,其抗压能力,性能如何这个暂不讨论,下面就分享一下 thinkphp强大的查询功能,当然这里只是抛砖引玉而已...


最近刚刚开始学习 thinkphp这套框架,对于thinkphp的强大真的是让哥叹为观止,其抗压能力,性能如何这个暂不讨论,下面就分享一下 thinkphp强大的查询功能,当然这里只是抛砖引玉而已。

一、带where条件的普通查询
 
1、字符串形式

$user=M('user');
$list=$user->where('id>5 and id<9')->select();
$list=$user->where($data)->select();

2、数组形式

$user=M('user');
$list=$user->where(array('username'=>'www.phpernote.com'))->select();
$list=$user->where($data)->select();

3、对象形式

$user=M('user');
$a=new stdClass();
$a->username='www.phpernote.com';
$list=$user->where($a)->select();   

4、查询表达式

EQ              等于
NEQ             不等于
GT              大于
EGT             大于等于
LT              小于
ELT             小于等于
LIKE            等价与sql中的like
[NOT] BETWEEN   查询区间
[NOT] IN        查询集合
EXP             指使用标准SQL语句,实现更加复杂的情况

语法格式:$data['字段名']=array('是表达式','查询条件');

例如

$data['username']='www.phpernote.com';

实际上是指

$data['username']=array('eq','www.phpernote.com');

$data['username']=array('like','peng%');
$list=$user->where($data)->select();

$data['username']=array(array('like','p%'),array('like','h%'),'or');
$list=$user->where($data)->select();

二、区间查询

$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();

三、组合查询

$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();

相当于

(id<30) and ((username=pengyanjie) or (password like p%))

五、统计查询

echo $user->count();
echo '
';
echo $user->max('id');
echo '
';
echo $user->where('id<30')->min('id');
echo '
';
echo $user->avg('id');
echo '
';
echo $user->sum('id');

六、定位查询

$user=new AdvModel('user');//实例化高级模型AdvModel
//$user=M('user','CommonModel');//或者将AdvModel用CommonModel来继承
$list=$user->order('id desc')->getN(2);//返回结果中的第三条
$list=$user->order('id desc')->last();//返回最后一条
$list=$user->order('id desc')->first();//返回第一条

七、SQL查询

excute()主要用于更新和写入

$Model=new Model()  //  实例化一个 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');

$user=new AdvModel('user');
$list=$user->top5();//前5条

分享到:
thinkphp在iis下的rewrite伪静态的配置图...
首先你要安装IIS下的rewrite组建,下载地址:Rewrite.zip 然后呢,在 IIS 管理器里选择网站,右键选择“属性”,如下图所示:(以下内容来自discuz帮助网站,你懂的) 在弹出的窗口里选择“ISAPI筛选器” 上图中点击“添加”,在弹出的窗口里“筛选器名称”填写“rewrite”   上图界面中点击“浏览”,选择下载解压后...
实例讲解ThinkPHP的UploadFile文件上传类...
ThinkPHP文件上传自带了上传类,使用起来非常方便,我们将以一个文件上传实例来讲解ThinkPHP上传类的实际用法,上传类使用时有详细的中文注释,可以非常方便的告诉你类的一些方法或变量的使用方法。   FileAction.class.php PHP Code复制内容到剪贴板   模板文件index.html   XML/HTML Cod...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……