sequlize

作者: 如果你还记得我是小双鱼 | 来源:发表于2018-10-26 18:09 被阅读0次

插入数据

    User.create({
        uid: 1233,
        name: 'Json',
        password: '13332'
    }) 

查询数据

  • findAll()

    1. 查询部分属性
      a. 查询属性(字段)可以通过传入一个嵌套数据进行重命名
        // 查询属性(字段)id, uid 
        User.findAll({
                attributes: ['id', ['uid', 'myUid']]
        })
        // { id: 1, myUid: 123 }
    
    1. 条件查询
        // 条件查询
        Model.findAll({
             where: {
                  attr1: {
                      $gt:  50  // > 大于50
                  },
                  attr2: {
                      $lte: 45   //  < 小于45
                  },
                  attr3: {
                      $in: [1,2,3]   
                  }
                  attr4: {
                      $ne:  5   //   !==5
                  },
                  attr5:  'cake'
             }
        })
        // WHERE attr1 > 50 AND attr2 < 45 AND attr3 in (1,2,3) AND attr4 !== 5 AND attr5 = 'cake'
    
    1. 聚合查询 sequelize.fn()
      a. 在使用聚合函数时,要给聚合字段指定一个别名
      b. 这样我们就能在查询的回调函数实例中通过instance.get('no_hats')来访问聚合统计的结果。
      c. 当需要查询所有字段并对某一字段使用聚合查询时,而只需要以对象的形式传入attributes并添加include子属性即可。
         Model.findAll({
             attributes: [[sequelize.fn('COUNT',  sequelize.col('hats') ), 'no_hats']]
         });
         // 这样会比较简短,且在你添加/删除属性后不会出错
         Model.findAll({
             attributes: { include: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }
         });
         
    
    1. 除去某个字段外查询全部字段 exclude
         Model.findAll({
             attributes: { exclude: ['baz'] }
         });
    
  • findById()

相关文章

  • sequlize

    插入数据 查询数据 findAll()查询部分属性a. 查询属性(字段)可以通过传入一个嵌套数据进行重命名 ...

  • sequlize - Op 模块

    ————————————————原文链接:https://blog.csdn.net/d649237053/art...

  • express安装sequlize框架

    sequlize是express的MySQL的对象模库,用于管理数据库的框架1,安装先cd到一个node.js的一...

  • v5 sequlize 使用$gt、$lt等失效显示‘=’ 原因

    问题背景:在写新项目时没有注意sequlize版本,发现$gt,$t这些条件都变成了‘=’ 如果版本大于v5的话,...

  • sequlize增删改查操作

    增、删、改、查 create 创建 destroy 删除 update 更新fields 指定更新的字段 find...

  • Sequelize使用

    Node.js 使用sequlize 操作mysql数据库时,查询一条记录中两个字段的加和官方文档中文文档 Seq...

  • node使用sequlize操作数据库

    看了很多node 架构服务的文章,跟大家都差不多。省时间,直接选择webstrom选择express模板下载下来了...

网友评论

      本文标题:sequlize

      本文链接:https://www.haomeiwen.com/subject/vexltqtx.html