select:普通查询,同 findAll() 方法
find:取得符合查询条件的一条记录
$list = $Dao->field('username,email')->select(); //查找某个字段
//where 等于
$condition['username'] = 'Admin';
$map['a'] =array('like',array('%thinkphp%','%tp'),'OR'); //like
$map['id'] = array('between','1,8'); //between
$map['id'] = array('not in','1,5,8'); //in / not in
$map['id'] = array('not in',array('1','5','8'));
$map['name|title'] = 'aa'; //or (name=aa or title = aa)
//'_multi'=>true必须加在数组的最后,表示当前是多条件匹配
//查询条件就变成 status= 1 AND score >0 AND title = 'thinkphp'
$map['status&score&title'] =array('1',array('gt','0'),'thinkphp','_multi'=>true); //and
//区间
$map['id'] = array(array('gt',1),array('lt',10)) ; // (`id` > 1) AND (`id` < 10)
$map['id'] = array(array('gt',3),array('lt',10), 'or') ; // (`id` > 3) OR (`id` < 10)
$map['id'] = array(array('neq',6),array('gt',3),'and'); //(`id` != 6) AND (`id` > 3)
$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')
$list = $Dao->where($condition)->select();
$list = $Dao->order('uid DESC')->select();
$list = $Dao->limit('4,5')->select();
$list = $Dao->order('uid DESC')->limit('4,5')->select();
---------------
//保存
$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data['name'] = 'ThinkPHP';
$data['score'] = array('exp','score+1');// 用户的积分加1
$User->where('id=5')->save($data);
//_string 包括字符串模式查询
$User = M("User"); // 实例化User对象
$map['id'] = array('neq',1);
$map['name'] = 'ok';
$map['_string'] = 'status=1 AND score>10';
$User->where($map)->select();
//请求字符串查询(_query)
$map['id'] = array('gt','100');
$map['_query'] = 'status=1&score=100&_logic=or'; //`id`>100 AND (`status` = '1' OR `score` = '100')
//Count() ,Max() ,Min(),Avg(),Sum()
$User = M("User");
$userCount = $User->count(); //所有用户数
$userCount = $User->count("id");
$minScore = $User->where('score>0')->min('score'); //获取积分大于0的用户的最小积分
$User->where('score>80')->order('score desc')->first(); //获取第一条记录
$User->where('score>80')->order('score desc')->last(); //获取最后一条记录
$User-> where('score>80')->order('score desc')->top8(); //前8条数据
//动态查询
//getBy+表中属性的名
$user = $User->getByName('liu21st');
//根据名子获得id
$user = $User->getFieldByName('liu21st','id');
//sql查询
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->query("select * from think_user where status=1");
//修改
$Model = new Model() // 实例化一个model对象 没有对应任何数据表
$Model->execute("update think_user set name='thinkPHP' where status=1");
$data['email'] = 'Jack@163.com';
$data['uid'] = 2;
$result = $Dao->save($data);
//删除
$result = $Dao->where('uid = 5')->delete();
$result = $Dao->where('status=0')->order('regdate ASC')->limit('5')->delete(); //删除多条
$db->where(array('id'=>array('in',$ids)))->delete();
$Dao = M();
//或者使用 $Dao = new Model();
$list = $Dao->query("select * from user where uid<5");
$where['name']=array('like',array('%jb51%','%.com'),'OR');
name like '%jb51%' or name like '%.com'
如果进行多字段查询,那么字段之间的默认逻辑关系是 逻辑与 AND,但是用下面的规则可以更改默认的逻辑判断,通过使用 _logic 定义查询逻辑:
$User = M("User"); // 实例化User对象
$condition['name'] = 'thinkphp';
$condition['account'] = 'thinkphp';
$condition['_logic'] = 'OR';
// 把查询条件传入查询方法
$User->where($condition)->select();
$User = M("User"); // 实例化User对象
$User->where('type=1 AND status=1')->select();