thinkjs mysql中crud
查询
let data = await model.where({deptno: 2}).find(); //根据条件查询
let data = await model.page(this.get('page')).countSelect();//?分页查询数据
let data = await model.limit(2).select();//查询多条数据
let data = await model.count();//获取总条数
let data = await model.max('deptno');//获取最大值
let data = await model.min('deptno');//获取最小值
let data = await model.avg('deptno');//获取平均值
let data = await model.sum('deptno');//获取字段总和
let data = await model.getField('deptno,dname,db_source',true);//获取指定字段值(单个或者多个字段,参数(字段,数量),第二个参数为true,则获取表里第一个字段值)
let data = await model.limit(1,2).select();//分页
let data = await model.page(2,2).select();//分页(当前页每页条数)
let data = await model.field('deptno AS dname,db_source').select();//查询匹配字段值,支持AS
let data = await model.fieldReverse('dname').select();//查询不匹配字段值,不支持AS
let data = await model.order(['deptno DESC,db_source ASC']).select();//升序降序,针对字符串,数组,对象三种书写方式
let data = await model.group('deptno').select();//分组查询
let aa=await this.model('student').alias('s').join({ //一对一连表查询
score:{ //表名
as:'sc',//别名
join:'inner',//类型
on:{//判断真假
's.id':'sc.stu_id'
}
}
}).select();
添加
await this.model('student').add({id:909,name:'杨哥',sex:'男',birth:1987,department:'计算机系',address:'山西省大同市'})//添加单条数据
await this.model('student').addMany ([//添加多条数据
{id:910,name:'啊哥',sex:'男',birth:1987,department:'计算机系',address:'山西省大同市'},
{id:911,name:'我哥',sex:'男',birth:1987,department:'计算机系',address:'山西省大同市'},
{id:912,name:'是哥',sex:'男',birth:1987,department:'计算机系',address:'山西省大同市'},
{id:913,name:'了哥',sex:'男',birth:1987,department:'计算机系',address:'山西省大同市'}
])
//第一个参数为要添加的数据,第二个参数为添加的条件,根据第二个参数的条件查询无相关记录时才会添加
let result = await model.thenAdd({id: 914, name:'了哥',birth:1987,department:'计算机系',address:'山西省大同市'}, {id: 914});
更新
await model.where({id: '914'}).update({name: 'aliyun'});//更新数据
await model.where('1=1').update({name: 'aliyun'});//更新所有数据
await model.where({id: 911}).increment('birth', 1); //将出生年加 1
await model.where({id: 912}).decrement('birth', 2); //将出生年减 2
修改
await model.where({id: ['>', 911]}).delete();//删除