如何将SQL连接查询转换为sequelizejs查询

时间:2019-03-14 11:45:56

标签: mysql node.js sequelize.js

我在一个在MuSQL工作台中可以正常运行的查询中遇到问题

SELECT * 
FROM coupons 
    LEFT JOIN coupon_activities ON coupons.coupon=coupon_activities.coupon 
ORDER BY coupon_activities.best_discount = true DESC, 
        coupon_activities.worked = true DESC, 
        coupons.expire_date DESC;

但是在使用sequelizejs的Node.js中,我遇到了麻烦。

这是优惠券模型表

module.exports = (sequelize, DataTypes) => {
    let coupon = sequelize.define("coupon", {
        merchant_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true
        },
        domain: {
            type: DataTypes.STRING,
        },
        coupon: {
            type: DataTypes.STRING,
            allowNull: false,
            primaryKey: true
        },
        description: {
            type: DataTypes.STRING,
        },
        expire_date: {
            type: DataTypes.DATE,
        }
    }, {
            indexes: [{ fields: ['merchant_id'] }],
            createdAt: false,
            updatedAt: false,
            tableName: 'coupons',
        });

    return coupon;
};

这是coupons_activity模型

module.exports = (sequelize, DataTypes) => {
  let couponActivity = sequelize.define("coupon_activity", {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      unique: true
    },
    coupon: {
      type: DataTypes.STRING,
      allowNull: false,
      primaryKey: true
    },
    merchant_id: {
      type: DataTypes.STRING,
      allowNull: false,
      primaryKey: true
    },
    application_date: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW
    },
    worked: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    best_discount: {
      type: DataTypes.BOOLEAN,
      defaultValue: false
    },
    discount_amount: {
      type: DataTypes.FLOAT,
      allowNull: true
    },
  }, {
    indexes: [{ fields: ['merchant_id'] }],
    createdAt: false,
    updatedAt: false,
    tableName: 'coupon_activities',
  });

  return couponActivity;
};

添加了关联

models.coupon.hasMany(models.coupon_activity, { as: 'Activity', foreignKey: 'merchant_id' });

这就是我在功能中所做的

try {
    const coupons = await models.coupon.findAll({
        include: [{
            model: models.coupon_activity,
            as: 'Activity'
        }]
    });
} catch (err) {
    console.log(err);
    res.status(500).send({ response: "Error in server" });
}

我正在从另一个表中获取所有数据,像这样的每个记录

 "coupons": [
    {
        "merchant_id": 19,
        "domain": "newegg.com",
        "coupon": "22WCPC101",
        "description": "$61.99 after savings. Plus, free shipping. ",
        "expire_date": "2019-03-31T00:00:00.000Z",
        "Activity": [
            {
                "id": 1,
                "coupon": "22WCPC101",
                "merchant_id": "19",
                "application_date": "2019-03-14T05:25:03.000Z",
                "worked": false,
                "best_discount": false,
                "discount_amount": 0
            },
            {
                "id": 2,
                "coupon": "353GAL34",
                "merchant_id": "19",
                "application_date": "2019-03-14T05:25:03.000Z",
                "worked": true,
                "best_discount": true,
                "discount_amount": 0
            },
            {
                "id": 3,
                "coupon": "EMCSTWTA2",
                "merchant_id": "19",
                "application_date": "2019-03-14T05:25:03.000Z",
                "worked": true,
                "best_discount": false,
                "discount_amount": 0
            },
            {
                "id": 4,
                "coupon": "BCPTAKE10",
                "merchant_id": "19",
                "application_date": "2019-02-14T05:25:03.000Z",
                "worked": false,
                "best_discount": false,
                "discount_amount": 0
            },
            {
                "id": 5,
                "coupon": "AFM2SSD26",
                "merchant_id": "19",
                "application_date": "2019-03-14T05:25:03.000Z",
                "worked": false,
                "best_discount": false,
                "discount_amount": 0
            },
            {
                "id": 6,
                "coupon": "EXTRATENOFF",
                "merchant_id": "19",
                "application_date": "2019-02-14T05:25:03.000Z",
                "worked": false,
                "best_discount": false,
                "discount_amount": 0
            }
        ]
    }

但是我想要的只是获取与两者的优惠券代码匹配的数据。然后需要对其进行排序。

1 个答案:

答案 0 :(得分:0)

您的关联使用'merchant_id':

models.coupon.hasMany(models.coupon_activity, { as: 'Activity', foreignKey: 'merchant_id' });

,但如果需要,应使用'coupon'

..coupons LEFT JOIN coupon_activities ON coupons.coupon=coupon_activities.coupon ...

FWIW,可以通过检查服务器日志中生成的SQL来解决许多Sequelize问题。