TP5的数据库配置文件:.../application/database.php
引用:https://www.kancloud.cn/manual/thinkphp5
基本操作:
Db::query('select * from think_user where id=?',[8]);
Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);
Db::query('select * from think_user where id=:id',['id'=>8]);
Db::execute('insert into think_user (id, name) values (:id, :name)',['id'=>8,'name'=>'thinkphp']);
Db::connect($config)->query('select * from think_user where id=:id',['id'=>8]);
$config是一个单独的数据库配置,支持数组和字符串,也可以是一个数据库连接的配置参数名。
查询语句:
查询单条记录:
Db::table('think_user')->where('id',1)->find();
find 方法查询结果不存在,返回 null,如果查询到了,返回数组。
查询多条记录:
Db::table('think_user')->where('status',1)->select();
select 方法查询结果不存在,返回空数组
如果设置了数据表前缀参数的话,可以使用
Db::name('user')->where('id',1)->find();
Db::name('user')->where('status',1)->select();
查询某个字段的值可以用
Db::table('think_user')->where('id',1)->value('name');
value 方法查询结果不存在,返回 null
查询某一列的值可以用
Db::table('think_user')->where('status',1)->column('name');
column 方法查询结果不存在,返回空数组
添加语句:
$data = ['foo' => 'bar', 'bar' => 'foo'];
Db::table('think_user')->insert($data);
$userId = Db::name('user')->getLastInsID(); //返回新增数据的自增主键
整合:
$userId =
Db::name('user')->insertGetId($data);
$data = [
['foo' => 'bar', 'bar' => 'foo'],
['foo' => 'bar1', 'bar' => 'foo1'],
['foo' => 'bar2', 'bar' => 'foo2']
];
Db::name('user')->insertAll($data);
insertAll 方法添加数据成功返回添加成功的条数
更新语句:
Db::table('think_user')->where('id', 1)->update(['name' => 'thinkphp']);
如果数据中包含主键,可以直接使用:
Db::table('think_user')->update(['name' => 'thinkphp','id'=>1]);
update 方法返回影响数据的条数,没修改任何数据返回 0
更新某个字段的值
Db::table('think_user')->where('id',1)->setField('name', 'thinkphp');
setField 方法返回影响数据的条数,没修改任何数据字段返回 0
自增或自减一个字段的值
// score 字段加 1
Db::table('think_user')->where('id', 1)->setInc('score');
// score 字段加 5
Db::table('think_user')->where('id', 1)->setInc('score', 5);
// score 字段减 1
Db::table('think_user')->where('id', 1)->setDec('score');
// score 字段减 5
Db::table('think_user')->where('id', 1)->setDec('score', 5);
删除语句:
// 根据主键删除
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);
// 条件删除
Db::table('think_user')->where('id',1)->delete();
Db::table('think_user')->where('id','<',10)->delete();
获取链式操作的SQL源生语句:
Db::table('think_user')->fetchSql(true)->delete(1);
Limit && page:
// 从ID=1,开始截取5条记录
Db::table('think_user')->limit(1,5)->select();
Db::table('think_user')->limit('1,5')->select();
// 截取5条记录
Db::table('think_user')->limit(5)->select();
// 从查询第一页数据,一页显示5条
Db::table('think_user')->page(1,5)->select();
Db::table('think_user')->page('1,5')->select();
Db::table('think_user')->limit(5)->page(1)->select();
引用:https://blog.youkuaiyun.com/haibo0668/article/details/78203170
where使用:
$map['字段名'] = array('表达式', '操作条件');
TP运算符 | SQL运算符 | 例子 | 实际查询条件 |
---|---|---|---|
eq | = | $map['id'] = array('eq',100); | 等效于:$map['id'] = 100; |
neq | != | $map['id'] = array('neq',100); | id != 100 |
gt | > | $map['id'] = array('gt',100); | id > 100 |
egt | >= | $map['id'] = array('egt',100); | id >= 100 |
lt | < | $map['id'] = array('lt',100); | id < 100 |
elt | <= | $map['id'] = array('elt',100); | id <= 100 |
like | like | $map<'username'> = array('like','Admin%'); | username like 'Admin%' |
between | between and | $map['id'] = array('between','1,8'); | id BETWEEN 1 AND 8 |
not between | not between and | $map['id'] = array('not between','1,8'); | id NOT BETWEEN 1 AND 8 |
in | in | $map['id'] = array('in','1,5,8'); | id in(1,5,8) |
not in | not in | $map['id'] = array('not in','1,5,8'); | id not in(1,5,8) |
and(默认) | and | $map['id'] = array(array('gt',1),array('lt',10)); | (id > 1) AND (id < 10) |
or | or | $map['id'] = array(array('gt',3),array('lt',10), 'or'); | (id > 3) OR (id < 10) |
xor(异或) | xor | 两个输入中只有一个是true时,结果为true,否则为false,例子略。 | 1 xor 1 = 0 |
exp | 综合表达式 | $map['id'] = array('exp','in(1,3,8)'); | $map['id'] = array('in','1,3,8'); |
引用:https://www.kancloud.cn/manual/thinkphp5
TP5链式操作
系统支持的链式操作方法有:
连贯操作 | 作用 | 支持的参数类型 |
---|---|---|
where* | 用于AND查询 | 字符串、数组和对象 |
whereOr* | 用于OR查询 | 字符串、数组和对象 |
wheretime* | 用于时间日期的快捷查询 | 字符串 |
table | 用于定义要操作的数据表名称 | 字符串和数组 |
alias | 用于给当前数据表定义别名 | 字符串 |
field* | 用于定义要查询的字段(支持字段排除) | 字符串和数组 |
order* | 用于对结果排序 | 字符串和数组 |
limit | 用于限制查询结果数量 | 字符串和数字 |
page | 用于查询分页(内部会转换成limit) | 字符串和数字 |
group | 用于对查询的group支持 | 字符串 |
having | 用于对查询的having支持 | 字符串 |
join* | 用于对查询的join支持 | 字符串和数组 |
union* | 用于对查询的union支持 | 字符串、数组和对象 |
view* | 用于视图查询 | 字符串、数组 |
distinct | 用于查询的distinct支持 | 布尔值 |
lock | 用于数据库的锁机制 | 布尔值 |
cache | 用于查询缓存 | 支持多个参数 |
relation* | 用于关联查询 | 字符串 |
with* | 用于关联预载入 | 字符串、数组 |
bind* | 用于数据绑定操作 | 数组或多个参数 |
comment | 用于SQL注释 | 字符串 |
force | 用于数据集的强制索引 | 字符串 |
master | 用于设置主服务器读取数据 | 布尔值 |
strict | 用于设置是否严格检测字段名是否存在 | 布尔值 |
sequence | 用于设置Pgsql的自增序列名 | 字符串 |
failException | 用于设置没有查询到数据是否抛出异常 | 布尔值 |
partition | 用于设置分表信息 | 数组 字符串 |
whereOR示例:
SQL:SELECT * FROM table_name WHERE id > 5 OR pid >10 ;
链式:Db::name('table_name')->where([id=>['>',5]])->whereOR(['pid'=>['>',10]])->select();
SQL:
SELECT * FROM table_name WHERE (id>=9 AND name='张三') OR (id<9 AND name="李四") ;
链式写法:
$where = [
'id' => ['>=', 9],
'name' => '张三'
];
$where_or = [
'id' => ['<', 9],
'name' => '李四'
];
Db::name('table_name')->WHERE(function($query) use ($where) {
$query->where($where);
})->whereOr(function($query) use ($where_or) {
$query->where($where_or);
})->select();
TP5聚合:
count | 统计数量,参数是要统计的字段名(可选) |
max | 获取最大值,参数是要统计的字段名(必须) |
min | 获取最小值,参数是要统计的字段名(必须) |
avg | 获取平均值,参数是要统计的字段名(必须) |
sum | 获取总分,参数是要统计的字段名(必须) |