PHP头条
热点:

ThinkPHP之查询语言


ThinkPHP中的查询语言:

普遍查询

1)字符串形式

$list=$user->where('username=bbbb')->select();

2)数组形式

  1. $data['username']='bbbbb'
  2. $list=$user->where($data)->select(); 

3)对象形式

  1. $user=M('user'); 
  2. $a=new stdClass(); 
  3. $a->username='bbbbb'
  4. $list=$user->where($a)->select(); 

查询表达式

EQ (=)

NEQ(!=)

GT (>) LT(<)  [NOT]BETWEEN(对应sql中的between) IN

EGT(>=)ELT(<=)LIKE (对应sql中的like)

EXP(使用标准sql语句实现较复杂的情况)

区间查询

  1. $map['id'] = array(array('gt',3),array('lt',10), 'or') ; 
  2. $map['name']  = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or'); 

对应的查询条件是:(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'ThinkPHP');

组合查询:

1)字符串模式查询(_string)

  1. $user=M('User'); 
  2.  $data[id]=array('neq',1); 
  3.  $data['username']='aaaaa'
  4.  $data['_string']='userpass=123 and createtime=2012'
  5.  $list=$user->where($data)->select(); 

2)请求字符串查询方式

  1. $data['id']=array('gt',100); 
  2. $data['_query']='userpass=1&username=aa&_logic=or'
  3. $list=$user->where($data)->select(); 

3)复合查询

  1. $wh['username']=array('like','%thinkphp%'); 
  2. $wh['userpass']=array('like','3%'); 
  3. $wh['_logic']='or'
  4. $data['_complex']=$wh
  5. $data['id']=array('gt',100); 
  6. $list=$user->where($data)->select(); 

对应于:(id>100)AND( (namelike'%thinkphp%')OR(titlelike'%thinkphp%') )

统计查询

  1. count():$num=$user->count();$num=$user->count('id'); 
  2. max(): 
  3. min(): 
  4. avg(): 
  5. sum(): 

定位查询

要求当前模型必须继承高级模型类才能使用

  1. $User->where('score>0')->order('score desc')->getN(2); 
  2. $User-> where('score>80')->order('score desc')->getN(-2); 
  3. $User->where('score>80')->order('score desc')->first(); 
  4. $User->where('score>80')->order('score desc')->last(); 

SQL查询

  1. $model=new Model(); 
  2. $list=$model->query("select * from think_user where id>1 and id<10"); 
  3. $model=new Model(); 
  4. $Model->execute("update think_user set name='thinkPHP' where status=1"); 

动态查询

  1. $user = $User->getByName('liu21st'); 
  2. $user = $User->getFieldByName('liu21st','id'); 
  3. $user-> where('score>80')->order('score desc')->top5(); 

www.phpzy.comtrue/phpkj/2384.htmlTechArticleThinkPHP之查询语言 ThinkPHP中的查询语言: 普遍查询 1)字符串形式 $list=$user->where(username=bbbb)->select(); 2)数组形式 $data [ username ]= bbbbb ; $list = $user ->where( $data )->select(); 3)对象形式 $user =M( us...

相关文章

相关频道:

PHP之友评论

今天推荐