手册目录
前言[随时更新]
MySQL快速复习
MySQL原生查询
选择数据表
删除数据
聚合查询
时间查询
视图查询
子查询
总结/参考
通过前面的学习,大家都知道了各种查询方法,都是间接通过调用Query类的相应方法来实现的,因为也完全可以直接使用Query类实例来完成查询。
······
//注释部分是我加上的,源码中是没有的
public function select($data = null)
{
if ($data instanceof Query) { //如果$data是Query类的实例
return $data->select(); //直接调用select方法获取结果集并返回
}
······
如何select参数是Query类实例,就直接调用该对象的select方法返回数据集。同理,如果前面有连贯操作一律无效,以Query对象调用的方法为主进行查询。
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;
class Index {
public function index(){
//1.创建Query对象
$query = new \think\db\Query();
//2.创建Query查询对象
$query -> table('tp5_staff')
-> field('id,name,salary')
-> where('salary','>',8000)
-> order('id desc')
-> limit(3);
//3.执行查询(select方法前不要有其它连贯方法,即使有也无效)
$result = Db::select($query);
//4.输出结果
dump($result);
}
}
array(3) {
[0] => array(3) {
["id"] => int(1028)
["name"] => string(6) "方方"
["salary"] => float(9877)
}
[1] => array(3) {
["id"] => int(1023)
["name"] => string(9) "段王爷"
["salary"] => float(9560)
}
[2] => array(3) {
["id"] => int(1011)
["name"] => string(9) "李云龙"
["salary"] => float(9850)
}
}
SELECT `id`,`name`,`salary` FROM `tp5_staff` WHERE `salary` > 8000 ORDER BY id desc LIMIT 3
Query对象查询方式,实际开发中用得不多,但提供给我们另一种选择
科技资讯
24小时阅读榜
1
2
3
4
5
6
7
8
9
10
精品课程
共5课时 | 18万人学习
共49课时 | 82.1万人学习
共29课时 | 65万人学习
共25课时 | 41.3万人学习
共43课时 | 76.7万人学习