标签:cti SQ man unique htm sele com func cut
findById:根据id查询表数据
Executing (default): SELECT "id", "account", "password" FROM "account" AS "account" WHERE "account"."id" = ‘test1‘;
Account.findById(‘test1‘, { raw: true }).then(rows => { console.log(rows); //{ id: ‘test1‘, account: ‘46191‘, password: ‘46191‘ } }, err => { console.log(err); });
findOne:根据条件查询表数据,只取一条
Executing (default): SELECT "id", "account", "password" FROM "account" AS "account" WHERE "account"."account" = ‘46192‘ LIMIT 1
Account.findOne({ where: { account: ‘46192‘ }, raw: true }).then(rows => { console.log(rows); //{ id: ‘test2‘, account: ‘46192‘, password: ‘46192‘ } }, err => { console.log(err); });
findOrCreate: 查找或创建,若存在符合条件的数据,则返回查询的值和false,不存在则创建数据并返回实例和true,适用单条数据。
Executing (2113e55f-3cf9-4e2f-a8e9-84f130a7f103): START TRANSACTION;
Executing (2113e55f-3cf9-4e2f-a8e9-84f130a7f103): SELECT "id", "account", "password" FROM "account" AS "account" WHERE "account"."account" = ‘46192‘ LIMIT 1;
Executing (2113e55f-3cf9-4e2f-a8e9-84f130a7f103): COMMIT;
Account.findOrCreate({ where: { account: ‘46192‘ }, defaults:{id: ‘test5‘}, raw: true }).then(rows => { //将then改为spread,返回值rows是(rows[0], rows[1]) console.log(rows); //[ { id: ‘test2‘, account: ‘46192‘, password: ‘46192‘ }, false ] }, err => { console.log(err); });
Executing (a6e8ce63-56c0-4d36-beb3-467ccde1b6e9): START TRANSACTION;
Executing (a6e8ce63-56c0-4d36-beb3-467ccde1b6e9): SELECT "id", "account", "password" FROM "account" AS "account" WHERE "account"."account" = ‘46195‘ LIMIT 1;
Executing (a6e8ce63-56c0-4d36-beb3-467ccde1b6e9): CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response "account", OUT sequelize_caught_exception text) RETURNS RECORD AS $func_c0fa0d6a908548f6a82c0b0de9611536$ BEGIN INSERT INTO "account" ("id","account") VALUES (‘test5‘,‘46195‘) RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_c0fa0d6a908548f6a82c0b0de9611536$ LANGUAGE plpgsql; SELECT (testfunc.response).*, testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();
Executing (a6e8ce63-56c0-4d36-beb3-467ccde1b6e9): COMMIT;
Account.findOrCreate({ where: { account: ‘46195‘ }, defaults:{id: ‘test5‘}, raw: true }).then(rows => { console.log(rows); //[ account实例, true] }, err => { console.log(err); });
findAndCount:查找并统计数量
Executing (default): SELECT "id", "account", "password" FROM "account" AS "account" WHERE "account"."id" IN (‘test1‘, ‘test2‘);
Account.findAndCount({ where: { id: [‘test1‘, ‘test2‘] }, raw: true }).then(res => { console.log(res); //{count: 2, rows:[{id: ‘test1‘, account: ‘46191‘, password: ‘46191‘},{id: ‘test2‘, account: ‘46192‘, password: ‘46192‘}]} }, err => { console.log(err); });
findAll:查找所有符合条件的数据
count:计算符合条件数据的总数,返回值是整数
max,min根据列名取最大最小值,若取不到返回null
Account.max(‘id‘).then(max => { console.log(max); //test7 }, err => { console.log(err); });
各种条件,where中能够实现and,or,>,<,in等
Account.findAll({ attributes: [], //属性 where: { //条件 id: ‘‘, account: ‘‘ }, group: ‘‘, //分组 order: ‘‘, //排序,可为数组 limit: 0, //查询条数,相当于sql的top offset: 0 //跳过多少条 }).then(rows => { console.log(rows); }, err => { console.log(err); });
标签:cti SQ man unique htm sele com func cut
原文地址:https://www.cnblogs.com/chenmengyuan/p/9036863.html