ThinkPhp学习08
一、普通查询方式
a、字符串
$arr=$m->where("sex=0 and username='gege'")->find();
b、数组
1 // 2.SELECT * FROM `tp_user` WHERE ( `sex` = 0 ) AND ( `username` = '李明2' ) 2 $data['sex']=0; 3 $data['username']='李明2'; 4 $arr=$m->where($data)->find(); 5 var_dump('$arr'); 6 $this->display();
find()只能查询一条,查询多条用select();
注意:这种方式默认是and的关系,如果使用or关系,需要添加数组值 $data['_logic']='or';
1 // 2.SELECT * FROM `tp_user` WHERE ( `sex` = 0 ) AND ( `username` = '李明2' ) 2 $data['sex']=0; 3 $data['username']='李明2'; 4 $data['_logic']='or'; 5 $arr=$m->where($data)->find(); 6 var_dump('$arr'); 7 $this->display();
二、表达式查询方式
$data['id']=array('lt',6); $arr=$m->where($data)->select();
EQ 等于 NEQ不等于 GT 大于 EGT大于等于 LT 小于 ELT小于等于
1 //2.SELECT * FROM `tp_user` WHERE ( `id` < 6 ) 2 $data['id']=array('lt',6); 3 $arr=$m->where($data)->select();
LIKE 模糊查询 $data['username']=array('like','%ge'); $arr=$m->where($data)->select();
NOTLIKE $data['username']=array('notlike','%ge%'); //notlike中间没有空格 $arr=$m->where($data)->select();
1 2.SELECT * FROM `tp_user` WHERE ( `username` LIKE '%李%' ) 2 $data['username']=array('like','%李%'); 3 $arr=$m->where($data)->select();
注意:如果一个字段要匹配多个通配符 $data['username']=array('like',array('%ge%','%2%','%五%'),'and');//如果没有第三个值,默认关系是or关系
$arr=$m->where($data)->select();
1 //2.SELECT * FROM `tp_user` WHERE ( (`username` LIKE '%李%' OR `username` LIKE '%2%') ) 2 //$arr= $data['username']=array('like',array('%李%','%2%'));// 3 //2.SELECT * FROM `tp_user` WHERE ( (`username` LIKE '%李%' AND `username` LIKE '%2%') ) 4 $arr= $data['username']=array('like',array('%李%','%2%'),'and');// 5 $arr=$m->where($data)->select(); 6 7 8 var_dump($arr);
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中间一定要有空格
1 //2.SELECT * FROM `tp_user` WHERE ( (`id` BETWEEN 1 AND 7 ) ) 2 // $data['id']=array('between',array(1,7)); 3 //SELECT * FROM `tp_user` WHERE ( (`id` NOT BETWEEN 1 AND 7 ) ) 4 $data['id']=array('not between',array(1,7)); 5 $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) )
1 //2.SELECT * FROM `tp_user` WHERE ( `id` IN (1,7) ) 2 //$data['id']=array('in',array(1,7)); 3 //2.SELECT * FROM `tp_user` WHERE ( `id` NOT IN (1,7) ) 4 $data['id']=array('not in',array(1,7)); 5 $arr=$m->where($data)->select();