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

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

table

table方法主要用于指定操作的数据表。

用法

  • 切换操作的数据表;
  • 对多表进行操作;

例如:

Db::table('think_user')->where('status>1')->select();

指定数据库:

Db::table('db_name.think_user')->where('status>1')->select();

简化数据表前缀:

Db::table('__USER__')->where('status>1')->select();
//会自动获取当前模型对应的数据表前缀来生成 think_user 数据表名称。

需要注意的是table方法不会改变数据库的连接,所以你要确保当前连接的用户有权限操作相应的数据库和数据表。
切换数据表后,系统会自动重新获取切换后的数据表的字段缓存信息。

对多表进行操作:

Db::field('user.name,role.title')
->table('think_user user,think_role role')
->limit(10)->select();

建议使用数组方式定义,可避免因表名和关键字冲突而出错:

Db::field('user.name,role.title')
->table(['think_user'=>'user','think_role'=>'role'])
->limit(10)->select();

猜你喜欢

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