多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
引入数据库:`use rp\\Db` * name(table):数据表,无需带前缀 * table(table):数据表,需带前缀,完整的数据表名称 * field(fieldStr):查询字段,字符串或一维数组,默认\* * alias(str):数据表别名,配合join使用 * join(table, condition, type):联表查询,join(关联表, 关联条件, 关联类型);类型有:left,right,inner,full 默认left;可传二维数组多表关联,如\[\[关联表, 关联条件, 关联类型\], \[关联表, 关联条件, 关联类型\]\] * where(str or array):查询条件,可字符串或二维数组,详细语法查看下方“where语法” * limit(limitStr):查询数量,如:limit('0,10') * order(field, type):排序方式,如:order('id','desc')或order(\['id'=>'desc','time'=>'desc'\]);使用SQL函数:order('rand()') * group(field):分组统计,如:group('type') * find():查询单条数据 * select():查询多条数据 * count(field):统计数量,默认字段\* * sum(field):获取总和 * insert(array):插入数据,一维数组时为单条,二维数组时为多条 * update(array):修改数据,如:update(\['name'=>'name1'\]) * setInc(field, number):指定字段自增,默认+1 * setDec(field, number):指定字段自减,默认-1 * dele():删除数据 #### 其他方法 * getSql():获取生成select的SQL语句 * insert\_id():获取新增数据的主键值 * affected\_rows():获取操作影响的行数 * query(sql):原生Mysql语句查询,方式:Db::instance()->query(sql语句); #### 快速使用 ~~~ //注意:必须Db::name()开始 $res=Db::name('tableName')->where(['id'=>1])->find(); ~~~ #### where语法 可用表达式:=, , >=, <, <=, like, not like, in, not in, between, not between, exists, not exists, exp, find\_in\_set 表达式参数请用字符串,比如array('id'=>array('in','1,2'))或array('id'=>array('in',join(',',\[1,2\])) 字符串形式: ~~~ Db::name('tableName')->where('id=1')->find(); ~~~ 常规用法: ~~~ Db::name('tableName')->where(array('id'=>1,'status'=>1))->find(); //生成SQL:select * from tableName where id = 1 and status = 1 ~~~ 表达式用法: ~~~ Db::name('tableName')->where(array('id'=>1,'status'=>array('>',1)))->find(); //生成SQL:select * from tableName where id = 1 and status > 1 ~~~ NULL用法: ~~~ Db::name('tableName')->where(array('name'=>'null'))->find(); //生成SQL:select * from tableName where name is null ~~~ NOT NULL用法: ~~~ Db::name('tableName')->where(array('name'=>'not null'))->find(); //生成SQL:select * from tableName where name is not null ~~~ 多字段同条件OR: ~~~ Db::name('tableName')->where(array('name|nick'=>'test'))->find(); //生成SQL:select * from tableName where (name = 'test' or nick = 'test') ~~~ 多字段同条件AND: ~~~ Db::name('tableName')->where(array('name&nick'=>'test'))->find(); //生成SQL:select * from tableName where (name = 'test' and nick = 'test') ~~~ 多字段同条件表达式: ~~~ Db::name('tableName')->where(array('name|nick'=>array('like','%t%')))->find(); //生成SQL:select * from tableName where (name like '%t%' or nick like '%t%') ~~~ 多字段不同条件OR: ~~~ Db::name('tableName')->where(array('name|nick'=>array('test','user')))->find(); //生成SQL:select * from tableName where (name = 'test' or nick = 'user') ~~~ 多字段不同条件AND: ~~~ Db::name('tableName')->where(array('name&nick'=>array('test','user')))->find(); //生成SQL:select * from tableName where (name = 'test' and nick = 'user') ~~~ 多字段不同条件表达式: ~~~ Db::name('tableName')->where(array('name|nick'=>array(array('<>','test'),array('like','%user%'))))->find(); //生成SQL:select * from tableName where (name <> 'test' or nick like '%user%') ~~~ 复杂逻辑: ~~~ Db::name('tableName')->where(array('status&(name|nick)'=>array(1,array('<>','test'),array('like','%user%'))))->find(); //生成SQL:select * from tableName where (status = 1 and (name <> 'test' or nick like '%user%')) ~~~ 多条件like: ~~~ Db::name('tableName')->where(array('(name|name)|(nick|nick2)'=>[['like','%user%'],['like','%member%'],['like','%user%'],['like','%member%']]))->find(); //生成SQL:select * from tableName where ((name like '%user%' or name like '%member%') or (nick like '%user%' or nick2 like '%member%')) ~~~ 混合使用: ~~~ Db::name('tableName')->where(array('phone'=>'not null','money'=>array('>',100),'status&(name|nick)'=>array(1,array('<>','test'),array('like','%user%'))))->find(); //生成SQL:select * from tableName where phone is not null and money > 100 and (status = 1 and (name <> 'test' or nick like '%user%')) ~~~ 注意:key为多字段时,如果value是字符串,则表示使用相同条件。如果是一维数组,则第一个字段对应value的第一个元素,以此轮推。同样value若是二维数组,第几个字段就对应value的第几个元素。 #### 事务 V1.9版本新增 ~~~ Db::transaction(); try{ /* *业务逻辑sql; */ // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } ~~~