查询方法
条件查询方法
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');
文档最后更新时间:2018-04-26 09:28:23
未解决你的问题?请到「问答社区」反馈你遇到的问题