您好,欢迎访问一九零五行业门户网

ThinkPHP数据库操作之视图查询、子查询、原生查询

下面由thinkphp教程栏目给大家介绍thinkphp数据库操作之视图查询、子查询、原生查询,希望对需要的朋友有所帮助!
视图查询
视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如:
db::view('user','id,name') ->view('profile','truename,phone,email','profile.user_id=user.id') ->view('score','score','score.user_id=profile.id') ->where('score','>',80) ->select();
生成的sql语句类似于:
select user.id,user.name,profile.truename,profile.phone,profile.email,score.score from think_user user inner join think_profile profile on profile.user_id=user.id inner join think_socre score on score.user_id=profile.id where score.score > 80
注意,视图查询无需调用 table 和 join 方法,并且在调用 where 和 order 方法的时候只需要使用字段名而不需要加表名。
默认使用inner join查询,如果需要更改,可以使用:
db::view('user','id,name') ->view('profile','truename,phone,email','profile.user_id=user.id','left') ->view('score','score','score.user_id=profile.id','right') ->where('score','>',80) ->select();
生成的sql语句类似于:
select user.id,user.name,profile.truename,profile.phone,profile.email,score.score from think_user user left join think_profile profile on profile.user_id=user.id right join think_socre score on score.user_id=profile.id where score.score > 80
可以使用别名:
db::view('user',['id'=>'uid','name'=>'account']) ->view('profile','truename,phone,email','profile.user_id=user.id') ->view('score','score','score.user_id=profile.id') ->where('score','>',80) ->select();
生成的sql语句变成:
select user.id as uid,user.name as account,profile.truename,profile.phone,profile.email,score.score from think_user user inner join think_profile profile on profile.user_id=user.id inner join think_socre score on score.user_id=profile.id where score.score > 80
可以使用数组的方式定义表名以及别名,例如:
db::view(['think_user'=>'member'],['id'=>'uid','name'=>'account']) ->view('profile','truename,phone,email','profile.user_id=member.id') ->view('score','score','score.user_id=profile.id') ->where('score','>',80) ->select();
生成的sql语句变成:
select member.id as uid,member.name as account,profile.truename,profile.phone,profile.email,score.score from think_user member inner join think_profile profile on profile.user_id=member.id inner join think_socre score on score.user_id=profile.id where score.score > 80
子查询
首先构造子查询sql,可以使用下面三种的方式来构建子查询。
1、使用 select 方法
当select方法的参数为false的时候,表示不进行查询只是返回构建sql,例如:
$subquery = db::table('think_user') ->field('id,name') ->where('id','>',10) ->select(false);
生成的subquery结果为:
select `id`,`name` from `think_user` where `id` > 10

2、使用 fetchsql 方法
fetchsql方法表示不进行查询而只是返回构建的sql语句,并且不仅仅支持select,而是支持所有的curd查询。
$subquery = db::table('think_user') ->field('id,name') ->where('id','>',10) ->fetchsql(true) ->select();
生成的subquery结果为:
select `id`,`name` from `think_user` where `id` > 10

3、使用 buildsql 构造子查询
$subquery = db::table('think_user') ->field('id,name') ->where('id','>',10) ->buildsql();
生成的subquery结果为:
( select `id`,`name` from `think_user` where `id` > 10 )
调用buildsql方法后不会进行实际的查询操作,而只是生成该次查询的sql语句(为了避免混淆,会在sql两边加上括号),然后我们直接在后续的查询中直接调用。
需要注意的是,使用前两种方法需要自行添加‘括号’。
然后使用子查询构造新的查询:
db::table($subquery.' a') ->where('a.name','like','thinkphp') ->order('id','desc') ->select();
生成的sql语句为:
select * from ( select `id`,`name` from `think_user` where `id` > 10 ) a where a.name like 'thinkphp' order by `id` desc
4、使用闭包构造子查询
in/not in 和 exists/not exists 之类的查询可以直接使用闭包作为子查询,例如:
db::table('think_user') ->where('id','in',function($query){ $query->table('think_profile')->where('status',1)->field('id'); }) ->select();
生成的sql语句是
select * from `think_user` where `id` in ( select `id` from `think_profile` where `status` = 1 )
db::table('think_user') ->where(function($query){ $query->table('think_profile')->where('status',1); },'exists') ->find();
生成的sql语句为
select * from `think_user` where exists ( select * from `think_profile` where `status`= 1 )
原生查询
db 类支持原生 sql 查询操作,主要包括下面两个方法:
query 方法
query 方法用于执行 sql 查询操作,如果数据非法或者查询错误则返回false,否则返回查询结果数据集(同 select 方法)。
使用示例:
db::query("select * from think_user where status=1");
如果你当前采用了分布式数据库,并且设置了读写分离的话,query方法始终是在读服务器执行,因此query方法对应的都是读操作,而不管你的sql语句是什么。
execute 方法
execute用于更新和写入数据的sql操作,如果数据非法或者查询错误则返回false ,否则返回影响的记录数。
使用示例:
db::execute("update think_user set name='thinkphp' where status=1");
如果你当前采用了分布式数据库,并且设置了读写分离的话,execute方法始终是在写服务器执行,因此execute方法对应的都是写操作,而不管你的sql语句是什么。
参数绑定
支持在原生查询的时候使用参数绑定,包括问号占位符或者命名占位符,例如:
db::query("select * from think_user where id=? and status=?",[8,1]);// 命名绑定db::execute("update think_user set name=:name where status=:status",['name'=>'thinkphp','status'=>1]);
以上就是thinkphp数据库操作之视图查询、子查询、原生查询的详细内容。
其它类似信息

推荐信息