【TP5 :数据库:查询构造器:链式操作】where

版权声明:本文为ywcmoon原创文章,未经允许不得转载。 https://blog.csdn.net/qq_39251267/article/details/82115443

where

where方法包括普通查询、表达式查询、快捷查询、区间查询、组合查询在内的查询操作
where方法的参数支持字符串和数组,虽然也可以使用对象但并不建议。

普通查询

最简单的数组查询方式如下:

$map['name'] = 'thinkphp';
$map['status'] = 1;
// 把查询条件传入查询方法
Db::table('think_user')->where($map)->select(); 

// 助手函数
db('user')->where($map)->select();

最后生成的SQL语句是

SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1

表达式查询

$map['id']  = ['>',1];
$map['mail']  = ['like','%[email protected]%'];
Db::table('think_user')->where($map)->select(); 

字符串条件

使用字符串条件直接查询和操作,例如:

Db::table('think_user')->where('type=1 AND status=1')->select(`); 

最后生成的SQL语句是

SELECT * FROM think_user WHERE type=1 AND status=1

使用字符串条件的时候,建议配合预处理机制,确保更加安全,例如:

Db::table('think_user')
    ->where("id=:id and username=:name")
    ->bind(['id'=>[1,\PDO::PARAM_INT],'name'=>'thinkphp'])
    ->select();

猜你喜欢

转载自blog.csdn.net/qq_39251267/article/details/82115443