原文:ThinkPhp学习09
三、区间查询
$data['id']=array(array('gt',4),array('lt',10));//默认关系是 and 的关系
//SELECT * FROM `tp_user` WHERE ( (`id` > 4) AND (`id` < 10) )
$data['id']=array(array('gt',4),array('lt',10),'or') //关系就是or的关系
1 //2.SELECT * FROM `tp_user` WHERE ( (`id` > 4) AND (`id` < 10) ) 2 $data['id']=array(array('gt',4),array('lt',10)); 3 //SELECT * FROM `tp_user` WHERE ( (`id` > 4) OR (`id` < 10) ) 4 //$data['id']=array(array('gt',4),array('lt',10),'or'); 5 $arr=$m->where($data)->select(); 6
$data['name']=array(array('like','%2%'),array('like','%五%'),'gege','or');
1 //2.SELECT * FROM `tp_user` WHERE ( (`username` LIKE '%2%') OR (`username` LIKE '%李%') OR (`username` = '李') ) 2 $data['username']=array(array('like','%2%'),array('like','%李%'),'李','or'); 3 $arr=$m->where($data)->select();
四、统计查询
count //获取个数
//2.SELECT COUNT(*) AS tp_count FROM `tp_user` WHERE ( `sex` = 0 ) $data['sex']=0; $arr=$m->where($data)->count();
max //获取最大数
//2.SELECT MAX(id) AS tp_max FROM `tp_user` echo $m->max('id');
min //获取最小数
//3.SELECT MIN(id) AS tp_min FROM `tp_user` echo $m->min('id');
avg //获取平均数 、sum //获取总和
1 //2.SELECT AVG(id) AS tp_avg FROM `tp_user` 2 echo $m->avg('id'); 3 //3.SELECT SUM(id) AS tp_sum FROM `tp_user` LIMIT 1 4 echo $m->sum('id'); 5 //var_dump($arr);
五、SQL直接查询
a、query 主要数处理读取数据的 成功返回数据的结果集 失败返回boolean false
1 //2.select * from tp_user where id >5 2 $result=$m->query("select * from tp_user where id >5"); 3 var_dump($result); 4
b、execute 用于更新个写入操作 成功返回影响行数 失败返回boolean false $m=M(); $result=$m->execute("insert into t_user(`username`) values('ztz3')"); var_dump($result);
//2.insert into tp_user(`username`) values('ztz3') $result=$m->execute("insert into tp_user(`username`) values('ztz3')"); var_dump($result);
时间: 2024-09-22 21:11:50