thinkphp普通查询与表达式查询实例分析

    //http://localhost/thinkphp323/index.php/home/User/inc_score?id=1&value=1
    public function inc_score($id=1,$value=1){
        $User = M("User"); // 实例化User对象
        $User->where('id=' . $id)->setInc('score',(int)$value); // 用户的积分加
    }

    //http://localhost/thinkphp323/index.php/home/User/dec_score?id=1&value=1
    public function dec_score($id=1,$value=1){
        $User = M("User"); // 实例化User对象
        $User->where('id=' . $id)->setDec('score',(int)$value); // 用户的积分加
    }

//$User = M("User"); // 实例化User对象
//$User->where('id=5')->setInc('score',3); // 用户的积分加3
//$User->where('id=5')->setInc('score'); // 用户的积分加1
//$User->where('id=5')->setDec('score',5); // 用户的积分减5
//$User->where('id=5')->setDec('score'); // 用户的积分减1

    //http://localhost/thinkphp323/index.php/home/User/select_score?score=100
    public function select_score($score=1){
        $User = M("User"); // 实例化User对象
        $u=$User->where('score=' . $score)->select();
        var_dump($u);
        
        $map['score'] = array('eq',$score);        
        $arr=$User->where($map)->select(); 
        var_dump($arr);

    }


    //http://localhost/thinkphp323/index.php/home/User/select_user_score?score=100&username=jim
    public function select_user_score($username='jim',$score=100){
        /*
        $User = M("User"); // 实例化User对象
        $condition['username'] = $username;
        $condition['score'] = $score;
        $condition['_logic'] = 'OR';
        */

        $User = M("User"); // 实例化User对象
        // 定义查询条件
        $condition = new \stdClass();
        $condition->username = $username;
        $condition->score = $score;
        $condition->_logic = 'OR';
        $User->where($condition)->select();

        // 把查询条件传入查询方法
        $u=$User->where($condition)->select();
        var_dump($u);
    }

thinkphp普通查询与表达式查询实例分析

投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-11-24 我要评论

这篇文章主要介绍了thinkphp普通查询与表达式查询,以实例形式较为详细的分析了thinkphp中的普通查询与表达式查询具体用法,包含普通查询的字符串方式与数组方式以及表达式查询中的各种常用技巧,非常具有实用价值,需要的朋友可以参考下
http://www.jb51.net/article/57755.htm
 

本文实例讲述了thinkphp普通查询与表达式查询。分享给大家供大家参考。具体分析如下:

一、普通查询方式

a、字符串方式:

复制代码代码如下:
$arr=$m->where("sex=0 and username='gege'")->find();//字符串需要加引号

 

b、数组方式:

复制代码代码如下:
$data['sex']=0;  
$data['username']='gege';  
$arr=$m->where($data)->find();//传上一个数组进行查询,这种方式默认是and(并且)的关系

注意:如果使用or关系,需要添加数组值
复制代码代码如下:
$data['sex']=0;  
$data['username']='gege';  
$data['_logic']='or';//对数组添加_logic赋值为or(或者)关系

 

二、表达式查询方式

复制代码代码如下:
$data['id']=array('lt',6);//数组的元素依然是数组  
$arr=$m->where($data)->select();  
/*
EQ 等于  //大小写没有关系  
NEQ不等于  
GT 大于  
EGT大于等于  
LT 小于  
ELT小于等于  
LIKE 模糊查询*/
$data['username']=array('like','%ge%');//like加通配符查询  
$arr=$m->where($data)->select();//所有包含ge的都查询出来 

 

//NOTLIKE不包含  
$data['username']=array('notlike','%ge%'); //notlike中间没有空格  
$arr=$m->where($data)->select(); 

//注意:如果一个字段要匹配多个通配符  
$data['username']=array('like',array('%ge%','%2%','%五%'),'and');//如果没有第三个值and,默认关系是or关系  
$arr=$m->where($data)->select();//或者(or)能查到其中一个值就行 

//BETWEEN  
$data['id']=array('between',array(5,7));  
$arr=$m->where($data)->select();  
//SELECT * FROM `tp_user` WHERE ( (`id` BETWEEN 5 AND 7 ) )  
$data['id']=array('not between',array(5,7));//注意,not 和 between中间一定要有空格  
$arr=$m->where($data)->select(); 

//IN  
$data['id']=array('in',array(4,6,7));  
$arr=$m->where($data)->select();  
//SELECT * FROM `tp_user` WHERE ( `id` IN (4,6,7) ) 

$data['id']=array('not in',array(4,6,7));  
$arr=$m->where($data)->select();  
//SELECT * FROM `tp_user` WHERE ( `id` NOT IN (4,6,7) )

 

希望本文所述对大家的thinkphp框架程序设计有所帮助。

如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!! 点击进入社区

 
 

 

posted @ 2017-11-27 19:07  sky20080101  阅读(64)  评论(0)    收藏  举报