简体   繁体   中英

Sequelize INNER JOIN

I need to Inner Join with sequelize. I tried something but i cant understand it.Anyone help me for this;

I have two model that are Users and Payments. I want to get Users with payments data.

I shared basicaly Example below Anyone explain for me? and how can i make it?

Totaly need ' SELECT * FROM Users INNER JOIN Payments ON Users.id=Payments.user_id WHERE Users.email=?';

 Users.findOne({ where:{ user_email:payload.email }, include:[{ model:Payments, where:['Payments.user_id = Users.id'] }] })

在此处输入图像描述

Update: Added this. After gived Error: Include unexpected. Element has to be either a Model, an Association or an object.

 Users.hasMany(Payments, { foreignKey: "user_id" }); Payments.belongsTo(Users, { foreignKey: "id" });

On your relation you can make it required for inner join.

Users.findOne({
    where: {
        user_email: payload.email
    },
    include: [{
        model: Payments,
        where: ['Payments.user_id = Users.id'],
        required: true
    }]
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM