Home >Backend Development >PHP Tutorial >Summary of query skills in ThinkPHP_PHP tutorial

Summary of query skills in ThinkPHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:11816browse

I have just started to learn the thinkphp framework recently. I am really amazed by the power of Thinkphp. I will not discuss its ability to withstand stress and its performance. Let me share the powerful query function of thinkphp. Of course, here It's just an introduction.

1. Ordinary query with where condition

1. String format

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

2. Array form

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

3. Object form

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

4. Query expression

EQ is equal to
NEQ is not equal to
GT Greater than
EGT Greater than or equal to
LT Less than
ELT Less than or equal to
LIKE is equivalent to like
in sql [NOT] BETWEEN Query range
[NOT] IN Query collection
EXP refers to using standard SQL statements to achieve more complex situations

Syntax format: $data['field name']=array('is an expression','query condition');

For example

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

actually means

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

2. Interval query

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

3. Combination query

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

4. Compound query

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

is equivalent to

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

5. Statistical query

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

6. Positioning query

$user=new AdvModel('user');//Instantiate the advanced model AdvModel
//$user=M('user','CommonModel');//Or inherit AdvModel with CommonModel
$list=$user->order('id desc')->getN(2);//Return the third item in the result
$list=$user->order('id desc')->last();//Return the last item
$list=$user->order('id desc')->first();//Return to the first item

7. SQL query

excute() is mainly used for updating and writing

$Model=new Model() // Instantiate a model object, which does not correspond to any data table
$Model->execute("update think_user set name='thinkPHP' where status=1");

query() is mainly used for query

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

8. Dynamic query

$user=M('user');

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

$user=new AdvModel('user');

$list=$user->top5();//Top 5 items

Articles you may be interested in

  • The last record of judging volist loop in thinkphp template
  • Summary of thinkphp development skills
  • Use of MVC pattern in php Tips
  • thinkphp like Query
  • System constant summary in the action controller of thinkphp
  • thinkphp method of intercepting Chinese strings
  • thinkphp automatic verification and automatic Solutions to invalid filling
  • Debugging methods that must be mastered when using ThinkPHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764123.htmlTechArticleI have just started to learn the thinkphp framework recently. I am really amazed by the power of Thinkphp and its ability to withstand pressure. , I won’t discuss the performance for now, let’s share some thoughts...
Statement:
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