1 select子句
指定查询需要的字段,1到无穷参数,每个参数为字段。
$db->user->select('id', 'name', 'email)->find();
另外当参数为一个数组且有2个元素时,为设置字段别名,第1个元素为字段名,第2个元素为别名。
$db->user->select('id', ['name', 'user_name'])->find();
另外当参数为一个数组且3个元素时,为设置字段聚合方法,,第1个元素为字段名,第2个元素聚合方法,第3个元素为别名。
通常需要配合group使用
$db->post->select(['user_id', 'count', 'user_post_count'])->group('user_id')->find();
2 order子句
指定查询排序,支持多个子句组合。
order有2个参数,第一个参数指定要排序的字段,第二参数决定排序类型(默认为false从小到大排序)
$db->user->order('name')->order('id', true)->find();
3 limit
限定查询结果
limit有1个参数时,限定只查询对应数目的结果。有2个参数时,第1个参数为起始数,第2个参数为限定查询结果数。
$db->user->limit(10)->find();
$db->post->limit(10, 10)->find();
4 page
page方法与limit类似,用于分页查询。
page方法第1个参数为页数,第2个参数为一页的记录数(默认为30)
// 查询第31到60条记录
$db->post->page(2)->find();
5 group
用于分组查询
// 查询每个用户发帖数
$db->post->select(['user_id', 'count', 'user_post_count'])->group('user_id')->find();
6 having
having与where类似只作用于group。