1,视图查询可以实现不依赖数据库视图的多表查询,并不需要数据库支持视图,例如:
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();注意,视图查询无需调用table和join方法,并且在调用where和order方法的时候只需要使用字段名而不需要加表名。
2,默认使用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();3,可以使用别名:
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();4.可以使用数组的方式定义表名以及别名,例如:
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();注意:
如果不需要设置表别名的话只需要填写模型名就行了,如:User
如果需要设置表别名,需要填写完整的数据库表名,如:think_user
官网手册地址:https://www.kancloud.cn/manual/thinkphp5_1/354031