ThinkPHP学习-4: ThinkPHP中的数据库查询

基本查询方式

条件对象查询

/*对象条件查询
        $condition = new\stdClass();
        $condition->id=2;
        $condition->user="zhangsan";
        $condition->_logic = 'OR';*/

数组对象查询

/*数组条件查询$condition['id']=1;*/
        //$map['id'] = array('eq',1);
        //$map['id'] = array('neq',1);
        //$map['id'] = array('gt',2);
        //$map['id'] = array('egt',2);
        //$map['id'] = array('lt',2);
        //$map['id'] = array('elt',2);
        //$map['user'] = array('like','%s%'); //查询包含s的用户名
        //$map['user'] = array('notlike','%san%'); //查询不包含san的用户名
        //$map['user'] = array('like',array('%s%','%l%'),'AND'); //查询包含s和l的用户名
        //$map['id'] = array('between','1,3'); //查询id在1和3之间的用户名(包含1和3)
        //$map['id'] = array('notbetween','1,3'); //查询id不在1和3之间的用户名(包含1和3),这样查询结果只有4
        //$map['id'] = array('in','1,3'); //查询符合指定调节的用户名,查询结果为1,3
        //$map['id'] = array('notin','1,3'); //查询符合指定调节的用户名,查询结果为2,4
        //$map['id'] = array('exp','<2');    //查询id小于2
        $map['id'] = array('exp','=1');
        $map['user'] = array('exp','="lisi"');
        $map['_logic'] = 'OR';
        $map['id'] = array('notin','1,3');
        var_dump($user->where($map)->select());

 

快捷查询方式

多字段查询

/$map['user|email']='zhangsan'; //多字段查询
        //SELECT * FROM `tp_user` WHERE ( `user` = 'zhangsan' OR `email` = 'zhangsan' )

多字段多关键字查询

$map['id|user']=array(1,'zhangsan','_multi'=>true); //多字段,多关键字查询。其中_multi表示一一对应的关键字

多字段多关键字加上表达式查询

    $map['id&user']=array(array('gt',0),'zhangsan','_multi'=>true); //多字段查询可以采用表达式方式
        //对应SQL语句:SELECT * FROM `tp_user` WHERE ( (`id` > 0) AND (`user` = 'zhangsan') ) 

 表达式组合查询

$map['id']=array(array('gt',0),array('lt',3));  //表达式组合查询
        //对应SQL语句:SELECT * FROM `tp_user` WHERE ( `id` > 0 AND `id` < 3 )

表达式组合查询--OR选项

$map['id']=array(array('gt',3),array('lt',1),'OR');  //表达式组合查询,or选项
        //对应SQL语句:SELECT * FROM `tp_user` WHERE ( `id` > 3 OR `id` < 1 ) 

字符串查询

$map['id']=array('eq',1);  //索引数组查询
        $map['_string']='user="zhangsan"AND email="[email protected]"';  //字符串查询
        //对应SQL语句:SELECT * FROM `tp_user` WHERE `id` = 1 AND ( user="zhangsan"AND email="[email protected]" )

 query方式查询

$map['id']=array('eq',1);  //索引数组查询
$map['_query'] = 'user=zhangsan&[email protected]';
//对应SQL语句:SELECT * FROM `tp_user` WHERE `id` = 1 AND ( `user` = 'zhangsan' AND `email` = '[email protected]' )

_complex符合表达式

//复合式
$map['id']=array('eq',1);  //索引数组查询
$where['id'] = 2;
$map['_complex']= $where;
$map['_logic']='OR';
//SQL: SELECT * FROM `tp_user` WHERE `id` = 1 OR ( `id` = 2 )

统计查询

数据总条数

var_dump($user->count()); 

 

字段总条数

var_dump($user->count(email)); 
//SELECT COUNT(email) AS tp_count FROM `tp_user` LIMIT 1

最大最小值

//var_dump($user->max(id)); //最大值,SQL:SELECT MAX(id) AS tp_max FROM `tp_user` LIMIT 1 
        var_dump($user->min(id));    //最小值:SQL:SELECT MIN(id) AS tp_min FROM `tp_user` LIMIT 1

平均值

var_dump($user->avg(id));    //平均值:SQL:SELECT AVG(id) AS tp_avg FROM `tp_user` LIMIT 1

总和

var_dump($user->sum(id));    //平均值:SELECT SUM(id) AS tp_sum FROM `tp_user` LIMIT 1

动态查询

//查询email账号信息
        //var_dump($user->getByEmail('[email protected]')); //SELECT * FROM `tp_user` WHERE `email` = '[email protected]' LIMIT 1
        //查询zhangsan的id信息
        var_dump($user->getFieldByUser('zhangsan','id'));  //SELECT `id` FROM `tp_user` WHERE `user` = 'zhangsan' LIMIT 1 

Query和Execute

var_dump($user->query('SELECT * FROM tp_user'));  //SELECT * FROM tp_user
//var_dump($user->execute('UPDATE tp_user set user="zhangsi" WHERE id=1'));  //UPDATE tp_user set user="zhangsi" WHERE id=1

 

 

 

 

你可能感兴趣的:(ThinkPHP学习-4: ThinkPHP中的数据库查询)