美文网首页
eggjs,分页条件查询,模糊查询,联合查询

eggjs,分页条件查询,模糊查询,联合查询

作者: 随风飞2019 | 来源:发表于2021-04-11 10:00 被阅读0次
  1. 分类,作者,来源,镇区,模型里关联新闻表
module.exports = (app) => {
    const { STRING, INTEGER } = app.Sequelize;
    const News = app.model.define(
        "News",
        {
            id: {
                type: INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            title: STRING,
            categoryLeave1: STRING,
            categoryLeave2: STRING,
            author: STRING,
            from: STRING,
            town: String,
            desc: String,
            imgList: String
        },
        {
            tableName: "sys_news"
        }
    );
    News.associate = function () {
        app.model.News.belongsTo(app.model.Category, { foreignKey: 'categoryLeave1', targetKey: 'value', 'as': 'categoryLeave1Obj' })
        app.model.News.belongsTo(app.model.Category, { foreignKey: 'categoryLeave2', targetKey: 'value', 'as': 'categoryLeave2Obj' })
        app.model.News.belongsTo(app.model.Author, { foreignKey: 'author', targetKey: 'id' })
        app.model.News.belongsTo(app.model.From, { foreignKey: 'from', targetKey: 'id' })
        app.model.News.belongsTo(app.model.Town, { foreignKey: 'town', targetKey: 'id' })
    }

    return News;
};

特别注意,belongsTo的用法,如果需要改名,需要使用as。这里使用了as,查询的地方也要对应使用as

  1. service里写查询语句,分页,条件,模糊,关联
async news(query) {
        let where = {}
        if (query.title) {
            where.title = { [Op.like]: '%' + query.title + '%' }
        }
        if (query.author) {
            where.author = query.author
        }
        if (query.from) {
            where.from = query.from
        }
        if (query.town) {
            where.town = query.town
        }
        if (query.categoryLeave1) {
            where.categoryLeave1 = query.categoryLeave1
            where.categoryLeave2 = query.categoryLeave2
        }
        const { count, rows } = await this.ctx.model.News.findAndCountAll({
            where,
            include: [
                { model: this.ctx.model.Category, attributes: ['label'], 'as': 'categoryLeave1Obj' },
                { model: this.ctx.model.Category, attributes: ['label'], 'as': 'categoryLeave2Obj' },
                { model: this.ctx.model.Author, attributes: ['label'] },
                { model: this.ctx.model.From, attributes: ['label'] },
                { model: this.ctx.model.Town, attributes: ['label'] }
            ],
            offset: (query.currentPage - 1) * query.pageSize,
            limit: parseInt(query.pageSize),
            order: [['updated_at', 'DESC']]
        })

        return {
            rows, count
        }
    }

相关文章

  • eggjs,分页条件查询,模糊查询,联合查询

    分类,作者,来源,镇区,模型里关联新闻表 特别注意,belongsTo的用法,如果需要改名,需要使用as。这里使用...

  • 数据库查询语句

    条件与逻辑查询 模糊查询 范围查询 排序 聚合函数 分组 连接查询 分页 子查询

  • hibernate中的查询

    HQL 查询所有 条件查询 分页查询 Criteria 查询所有 条件查询 分页查询 查询总记录 原生SQL

  • 像查询DB一样查询redis

    设计目的:希望查询redis缓存像查询数据库一样,支持多条件组合查询、模糊查询、区间查询、多字段排序查询、分页查询...

  • 05-Mysql数据库02

    mysql数据查询 条件查询 模糊查询 范围查询 判断空 排序 聚合函数 分组 分组要和聚合一起用 分页 连接查询...

  • MySQL的简单查询语句

    查询: 一:查询所有数据 二:根据条件查 三:模糊查询 四:排序 五:统计函数(聚合函数) 六:分组查询 七:分页...

  • mysql数据库中的多表关联查询

    条件查询 查询全部 查询单独信息 条件查询(有范围) 模糊查询 (like) 排序(order by) 查询我们...

  • Mybatis-其他查询

    模糊查询 多条件查询

  • Laravel UNION联合查询并分页

    UNION联合查询并分页 联合查询用于将两个或更多查询的结果集组合为单个结果集,该结果集包含联合查询中所有查询的全...

  • MySQL查询语句

    一、基本查询: 二、限制查询:where 三、排序查询:order by 四、模糊查询:like 五、模拟分页:l...

网友评论

      本文标题:eggjs,分页条件查询,模糊查询,联合查询

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