栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > thinkphp

视图查询 - ThinkPHP5.0完全开发手册

thinkphp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

视图查询 - ThinkPHP5.0完全开发手册

视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如: ~~~ 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 ~~~
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/213517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号