ThinkPHP中的常用查询语言汇总_PHP

WBOY
Release: 2016-05-31 19:30:05
Original
947 people have browsed it

ThinkPHP

本文实例汇总了ThinkPHP中的常用查询语言,供大家参考之用。相信能给大家ThinkPHP开发带来一定的帮助。具体如下:

一、普通查询:

在查询带入where条件等,最少有三种形式

1、字符串形式:

'id>5 and id<9'

Copy after login

2、数组形式:

示例代码如下:

$user=M('user');
$data['username']='liwenkai';
$list=$user->where(array('username'=>'liwenkai'))->select();
$list=$user->where($data)->select();

Copy after login

3、对象形式:

示例代码如下:

$user=M('user');
$a=new stdClass();
$a->username='liwenkai';
$list=$user->where($a)->select();  

Copy after login

4、查询表达式:

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

常用形式:

$data['字段名']=array('是表达式','查询条件');
Copy after login

此外

$data['liwenkai']='liwenkai';
Copy after login

实际上等价于

$data['liwenkai']=array('eq','liwenkai');
Copy after login

示例如下:

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

Copy after login

二、区间查询:

示例如下:

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

$data['username']=array(array('like','p%'),array('like','h%'),'or');

Copy after login

三、组合查询:

示例如下:

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

Copy after login

四、复合查询:

示例如下:

$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);
Copy after login

相当于

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

五、统计查询:

示例如下:

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');

Copy after login

六、定位查询:

示例如下:

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

$list=$user->order('id desc')->last();//返回最后一条
$list=$user->order('id desc')->first();//返回第一条

Copy after login

七、SQL查询:

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

$Model = new Model() // 实例化一个 model 对象  没有对应任何数据表
$Model->execute( "update think_user set name='thinkPHP' where status=1" );

Copy after login

2.query()主要用于查询:

$user=M();
$list=$user->query('select * from aoli_user order by id desc');
dump($list);         
Copy after login

八、动态查询

示例如下:

$user=M('user');
$list=$user->getByusername('pengyanjie');
$list=$user->getByusername('pengyanjie');
dump($list);

Copy after login

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

Copy after login

感兴趣的朋友可以在ThinkPHP项目中调试运行本文示例,相信会有新的收获。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!