php sql语句高级查询,thinkphp5.1高级查询

namespace app\index\model;

use think\Model;

use think\Db;

use think\db\Where;

class Articles extends Model {

/**

* 高级查询-快捷查询方式是一种多字段相同查询条件的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询

*/

public function indexFun(){

// 快捷查询-快捷查询方式是一种多字段相同查询条件的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询

$rs = Db::table('zht_articles')

->where('articleTitle|articleContent', 'like', 'thinkphp')

->where('createTime&updateTime', '>', '2016-1-1')

->find();

// 区间查询-区间查询是一种同一字段多个查询条件的简化写法

// 区间查询的查询条件必须使用数组定义方式,支持所有的查询表达式。

$rs = Db::table('zht_articles')

->where('articleTitle', ['like', '%thinkphp%'], ['like', '%kancloud%'],'OR')

->where('articleId', ['>',0], ['<>',10], 'AND')

->find();

// 区间查询其实可以用下面的方式替代,更容易理解,因为查询构造器支持对同一个字段多次调用查询条件

$rs = Db::table('zht_articles')

->where('articleTitle', 'like', '%thinkphp%')

->where('articleTitle', 'like', '%kancloud%')

->where('articleId', '>',0)

->where('articleId', 'in',[1,3,5,7,9])

->find();

// 批量(字段)查询-可以进行多个条件的批量条件查询定义

$rs = Db::table('zht_articles')

->where([['articleTitle', 'like', '%thinkphp%'],['articleId', '>',0]])

->find();

// 注意,V5.1.7+版本数组方式如果使用exp查询的话,一定要用raw方法。

$rs = Db::table('zht_articles')

->where([['articleTitle', 'like', '%thinkphp%'],['articleId', 'exp', Db::raw('>clickNum')]])

->find();

// 数组查询方式,确保你的查询数组不能被用户提交数据控制,用户提交的表单数据应该是作为查询数组的一个元素传入

$title = 'thinkphp';

$id = 10;

$rs = Db::table('zht_articles')

->where([['articleTitle', 'like', '%'.$title.'%'],['articleId', '>',$id]])

->find();

// 注意,相同的字段的多次查询条件可能会合并,如果希望某一个where方法里面的条件单独处理,可以使用下面的方式,避免被其它条件影响。

$map = [['articleTitle', 'like', '%thinkphp%'],['articleId', '>',0]];

$rs = Db::table('zht_articles')

->where([$map])

->where('clickNum',1)

->find();

// 如果使用下面的多个条件组合-善用多维数组查询,可以很方便的拼装出各种复杂的SQL语句

$map1 = [['articleTitle', 'like', '%thinkphp%'],['articleContent', 'like', '%thinkphp%']];

$map2 = [['articleTitle', 'like', '%kancloud%'],['articleContent', 'like', '%kancloud%']];

$rs = Db::table('zht_articles')

->whereOr([$map1,$map2])

->find();

// 数组对象查询(V5.1.21+)

$map = [

'articleTitle' => ['like', 'thinkphp%'],

'articleContent' => ['like', '%think%'],

'articleId' => ['>', 10],

'clickNum' => 1,

];

$where = new Where;

$where['articleId'] = ['in', [1, 2, 3, 5, 8]];

$where['articleTitle'] = ['like', '%php%'];

$rs = Db::table('zht_articles')

->where(new Where($map))

->whereOr($where->enclose()) // enclose方法表示该查询条件两边会加上括号包起来。

->select();

// 闭包查询-可见每个闭包条件两边也会自动加上括号,但需要注意,使用闭包查询的时候不能使用cache(true)数据缓存,而应该使用指定key的方式例如cache('key')。

$name = 'thinkphp';

$id = 10;

$rs = Db::table('zht_articles')->where(function ($query) use($name, $id) {

$query->where('articleTitle', $name)

->whereOr('articleId', '>', $id);

})->select();

// 混合查询

$rs = Db::table('zht_articles')

->where('articleTitle', ['like', 'thinkphp%'], ['like', '%thinkphp'])

->where(function ($query) {

$query->where('articleId', ['', 100], 'or');

})

->select();

// 字符串条件查询

$rs = Db::table('zht_articles')

->where('articleTitle LIKE "%thinkphp%" AND articleId>10')

->select();

// 为了安全起见,我们可以对字符串查询条件使用参数绑定

$rs = Db::table('zht_articles')

->where('articleTitle LIKE :name AND articleId>:id',['id' => 0, 'name' => 'thinkphp%'])

->select();

// V5.1.8+版本开始,如果你要使用字符串条件查询,推荐使用whereRaw

$rs = Db::table('zht_articles')

->whereRaw('articleTitle LIKE :name AND articleId>:id',['id' => 0, 'name' => 'thinkphp%'])

->select();

// 使用Query对象查询(V5.1.5+)-V5.1.5+版本开始,可以通过调用一次where方法传入Query对象来进行查询。

// Query对象的where方法仅能调用一次,如果query对象里面使用了非查询条件的链式方法,则不会传入当前查询。

$query = new \think\db\Query;

$query->where('articleId','>',0)

->where('articleTitle','like','%thinkphp');

$rs = Db::table('zht_articles')

->where($query)

->select();

// 快捷方法

// 查询update_time大于create_time的用户数据

$rs = Db::table('zht_articles')

->whereColumn('updateTime','>','createTime')

->select();

// 查询name和nickname相同的用户数据

$rs = Db::table('zht_articles')

->whereColumn('articleTitle','=','articleContent')

->select();

// 相同字段条件也可以简化为

$rs = Db::table('zht_articles')

->whereColumn('articleTitle','articleContent')

->select();

// V5.1.11+版本开始,支持数组方式比较多个字段

$rs = Db::table('zht_articles')

->whereColumn([['articleTitle','=','articleContent'],['updateTime','>=','createTime']])

->select();

// 动态查询-查询构造器还提供了两个动态查询机制,用于简化查询条件,包括getBy和getFieldBy。

// 其中FieldName表示数据表的实际字段名称的驼峰法表示,假设数据表user中有email和nick_name字段,我们可以这样来查询。

// getBy和getFieldBy方法只会查询一条记录,可以和其它的链式方法搭配使用

// 根据邮箱(email)查询用户信息

$rs = Db::table('zht_articles')

->whereEmail('thinkphp@qq.com')

->find();

// 根据昵称(nick_name)查询用户

$rs = Db::table('zht_articles')

->whereNickName('like', '%流年%')

->find();

// 根据邮箱查询用户信息

$rs = Db::table('zht_articles')

->getByEmail('thinkphp@qq.com');

// 根据昵称(nick_name)查询用户信息

$rs = Db::table('zht_articles')

->field('articleId,articleTitle')

->getByNickName('流年');

// 根据邮箱查询用户的昵称

$rs = Db::table('zht_articles')

->getFieldByEmail('thinkphp@qq.com','nick_name');

// 根据昵称(nick_name)查询用户邮箱

$rs = Db::table('zht_articles')

->getFieldByNickName('流年', 'email');

// 条件查询

// 5.1的查询构造器支持条件查询

$condition = true;

$rs = Db::table('zht_articles')->when($condition, function ($query) {

// 满足条件后执行

$query->where('clickNum', '>', 10)->limit(10);

})->select();

// 并且支持不满足条件的分支查询

$condition = false;

$rs = Db::table('zht_articles')->when($condition, function ($query) {

// 满足条件后执行

$query->where('clickNum', '>', 10)->limit(10);

}, function ($query) {

// 不满足条件执行

$query->where('clickNum', '>', 1);

})->select();

// echo \think\facade\App::version(); //输出thinkphp5.1版本号

echo Db::getlastsql();

dump($rs);

exit;

return $rs;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值