【TP5:数据库:查询构造器】查询方法

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

查询方法

where方法

使用where方法进行AND条件查询:

Db::table('think_user')
    ->where('name','like','%thinkphp')
    ->where('status',1)
    ->find();

多字段相同条件的AND查询可以简化为如下方式:

Db::table('think_user')
    ->where('name&title','like','%thinkphp')
    ->find();

whereOr方法

使用whereOr方法进行OR查询:

Db::table('think_user')
    ->where('name','like','%thinkphp')
    ->whereOr('title','like','%thinkphp')
    ->find();

多字段相同条件的OR查询可以简化为如下方式:

Db::table('think_user')
    ->where('name|title','like','%thinkphp')
    ->find();

混合查询

wherewhereOr 混合使用

$result = Db::table('think_user')->where(function ($query) {
    $query->where('id', 1)->whereor('id', 2);
})->whereOr(function ($query) {
    $query->where('name', 'like', 'think')->whereOr('name', 'like', 'thinkphp');
})->select();

生成的sql语句类似于下面:

SELECT * FROM `think_user` WHERE  (  `id` = 1 OR `id` = 2 ) OR (  `name` LIKE 'think' OR `name` LIKE 'thinkphp' )

注意:闭包查询里面的顺序,而且第一个查询方法用 where 或者 whereOr 是没有区别的。


getTableInfo方法

使用getTableInfo可以获取表信息,信息类型 包括 fields,type,bind,pk
以数组的形式展示,可以指定某个信息进行获取

// 获取`think_user`表所有信息
Db::getTableInfo('think_user');
// 获取`think_user`表所有字段
Db::getTableInfo('think_user', 'fields');
// 获取`think_user`表所有字段的类型
Db::getTableInfo('think_user', 'type');
// 获取`think_user`表的主键
Db::getTableInfo('think_user', 'pk');

猜你喜欢

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