查詢方法
條件查詢方法
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();
混合查詢
where方法和whereOr方法在復雜的查詢條件中經常需要配合一起混合使用,下面舉個例子:
$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');