简体   繁体   中英

SQL query to Sequelize query

select *
From Visitors
join CsvData on
CsvData.fileName = Visitors.fileName

How to make join like above.

db.Visitors.find({
    include:[{
        model: CsvData,
        joinCondition: {
            Visitors.fileName : CsvData.fileName  //*wrong syntax but rough idea
        }
    }]
})

Any help will be appreciated

You're almost there :) You first want to associate the two models. Assuming fileName is defined as a primary key on CsvData :

Visitors.hasMany(CsvData, {
  as: 'CsvDataItems',
  foreignKey: 'fileName'
});

Visitors.findAll({
  include:[{
    model: CsvData,
    as: 'CsvDataItems' // Must match the "as" specified in the association above
  }]
}).then(function(results) {
  console.log('Success!', results);
}).catch(function(err) {
  console.error('Something went wrong', err);
});

Note that if you wanted a right join (I know you didn't ask, but hey) you can add required: true to the include object.

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