[size=medium]ThinkPHP 3.1.2 数据库查询方式[/size]
[size=medium][b]1.普通查询方式;[/b][/size]
[size=small] a.字符串
$arr = $m->where('id = 1 and username="张三"')->select();
b.数组(建议使用)
$data['age'] = 12;
$data['username'] = '李四';
$data['_logic'] = 'or'; //默认方式:$data['_logic'] = 'and';
$arr = $m->where($data)->find();[/size]
[size=medium][b]2.表达式(大小写不敏感)[/b][/size]
[size=small]EQ 等于
NEQ 不等于
GT 大于
EGT 大于等于
LT 小于
ELT 小于等于
如:$data['id'] = array('egt',2);
$m->where($data)->select();
LIKE模糊查询
如$data['username'] = array('like','张%');
$arr = $m->where($data)->select();
NOTLIKE
如:$data['username'] = array('notlike','张%'); //注意notlike中间没有空格
$arr = $m->where($data)->select();
注意:如果一个字段要匹配多个通配符,其形式如下:
$data['username'] = array('like',array('张%','李%'),'or'); //找到姓张的或者姓李的
$arr = $m->where($data)->select();
BETWEEN
$data['id'] = array('between',array('2','4'));
$arr = $m->where($data)->select();
NOT BETWEEN
$data['id'] = array('not between',array('2','4')); //注意not between中间带空格
$arr = $m->where($data)->select();
IN
$data['id'] = array('in',array(2,4));
$arr = $m->where($data)->select();[/size]
[size=medium][b]三、区间查询[/b][/size]
[size=small]1.$data['id'] = array(array('gt',2),array('lt',6)); //第三个参数默认是and
2.$data['id'] = array(array('lt',2),array('gt',6),'or'); //第三个参数手动指定or时
3.$data['username'] = array(array('like','%张%'),array('like','%李%'),array('eq','xqlg'),'or');//形成的sql语句形如:SELECT * FROM `tp_user` WHERE ( (`username` LIKE '%张%') OR (`username` LIKE '%李%') OR (`username` = 'xqlg') )[/size]
[size=medium][b]四、统计查询[/b][/size]
[size=small]count 获取数据条数
max 获取最大值
如:$m->max('id');$m->where('age=100')->count();
min 获取最小值
avg 获取平值均
sum 获取总和[/size]
[size=medium][b]五、sql直接查询[/b][/size]
[size=small]a.query 主要是读取数据,成功返回结果集,失败返回false
如:$m->query('select * from tp_user where id > 1');
b.execute 主要是执行sql语句,成功返回影响的行数,失败返回false
如:$m->execute('insert into tp_user(`username`) values("老六")');[/size]
[size=medium][b]1.普通查询方式;[/b][/size]
[size=small] a.字符串
$arr = $m->where('id = 1 and username="张三"')->select();
b.数组(建议使用)
$data['age'] = 12;
$data['username'] = '李四';
$data['_logic'] = 'or'; //默认方式:$data['_logic'] = 'and';
$arr = $m->where($data)->find();[/size]
[size=medium][b]2.表达式(大小写不敏感)[/b][/size]
[size=small]EQ 等于
NEQ 不等于
GT 大于
EGT 大于等于
LT 小于
ELT 小于等于
如:$data['id'] = array('egt',2);
$m->where($data)->select();
LIKE模糊查询
如$data['username'] = array('like','张%');
$arr = $m->where($data)->select();
NOTLIKE
如:$data['username'] = array('notlike','张%'); //注意notlike中间没有空格
$arr = $m->where($data)->select();
注意:如果一个字段要匹配多个通配符,其形式如下:
$data['username'] = array('like',array('张%','李%'),'or'); //找到姓张的或者姓李的
$arr = $m->where($data)->select();
BETWEEN
$data['id'] = array('between',array('2','4'));
$arr = $m->where($data)->select();
NOT BETWEEN
$data['id'] = array('not between',array('2','4')); //注意not between中间带空格
$arr = $m->where($data)->select();
IN
$data['id'] = array('in',array(2,4));
$arr = $m->where($data)->select();[/size]
[size=medium][b]三、区间查询[/b][/size]
[size=small]1.$data['id'] = array(array('gt',2),array('lt',6)); //第三个参数默认是and
2.$data['id'] = array(array('lt',2),array('gt',6),'or'); //第三个参数手动指定or时
3.$data['username'] = array(array('like','%张%'),array('like','%李%'),array('eq','xqlg'),'or');//形成的sql语句形如:SELECT * FROM `tp_user` WHERE ( (`username` LIKE '%张%') OR (`username` LIKE '%李%') OR (`username` = 'xqlg') )[/size]
[size=medium][b]四、统计查询[/b][/size]
[size=small]count 获取数据条数
max 获取最大值
如:$m->max('id');$m->where('age=100')->count();
min 获取最小值
avg 获取平值均
sum 获取总和[/size]
[size=medium][b]五、sql直接查询[/b][/size]
[size=small]a.query 主要是读取数据,成功返回结果集,失败返回false
如:$m->query('select * from tp_user where id > 1');
b.execute 主要是执行sql语句,成功返回影响的行数,失败返回false
如:$m->execute('insert into tp_user(`username`) values("老六")');[/size]