thinkphp sql语句查询

定点查询

在controller中****controller.class.php中

这种字符索引安全性不高

$user = M('user');
        var_dump($user->where('id=1')->select());



数组索引(比字符索引安全)

 $condition['id'] = 1;
        var_dump($user->where($condition)->select());


sql语句的and

$condition['id'] = 1;
$condition['user'] = '蜡笔小新';
var_dump($user->where($condition)->select());


sql语句or

$condition['id'] = 1;
$condition['user'] = '蜡笔小新';
$condition['_logic'] = 'OR';
var_dump($user->where($condition)->select());


适用对象查询

$condition = new \stdClass();
var_dump($user->where($condition)->select());

其中stdClass是tp中的内置类必须在根目录下寻找所以要加 \



缺少‘\’



$condition = new \stdClass();
$condition->id = 1;
$condition->user = '蜡笔小新';
var_dump($user->where($condition)->select());


表达式查询


eq

$map['id'] = array('eq',1);
var_dump($user->where($map)->select());


neq


gt



egt


like模糊查找

 $map['user'] = array('like','%小%');



like数组查询

$map['user'] = array('like',array('%小%','%蜡%'),'AND');



如果array()后没跟and就是or查询

$map['user'] = array('like',array('%小%','%蜡%'));


between区间查询

$map['id'] = array('between',array('1','3'));

与这条语句等效

$map['id'] = array('between','1,3');



in区域查询

$map['id'] = array('in','1,2,4');



自定义查询

$map['id'] = array('exp','=1');

$map['id'] = array('eq',1);

相同


$map['id'] = array('exp','>1');

$map['id'] = array('gt',1);

相同

快捷查询

$user = M('User');
$map['user|email'] = '蜡笔小新';
var_dump($user->where($map)->select());


'|'=or    '$'=and     





猜你喜欢

转载自blog.csdn.net/qq_39248122/article/details/80410111