如何使用 Sequelize 从同一个表中选择两次? 这是 MySQL 代码:
select b.*, a.parent_id
from theSameTable as b left join theSameTable as a on b.parent_id = a.id
这是我的 MySQl 表
这是我的 Sequelize 代码
const db = await ec.sequelize.define(tableDb, {
id: {
type: ec.Sequelize.INTEGER.UNSIGNED,
primaryKey: true,
autoIncrement: true
},
url: ec.Sequelize.STRING(511),
url_hash: ec.Sequelize.STRING(32),
name: ec.Sequelize.STRING(511),
full_name: ec.Sequelize.STRING(511),
parent_id: ec.Sequelize.INTEGER(11).UNSIGNED,
cnt: ec.Sequelize.STRING(255),
chk: ec.Sequelize.INTEGER(1)
}, {
indexes: [{
unique: true,
fields: ['url_hash']
}]
});
await ec.sequelize.sync();
最佳答案
终于找到答案了。也许对某人来说会有用。 之后
await ec.sequelize.sync();
写
db.belongsTo(db, {
as: 'db2',
foreignKey: 'parent_id',
required: false
});
let rows = await db.findAll({
where: {
chk: 0
},
include: [{
model: db,
as: 'db2',
attributes: ['id', 'full_name']
}],
raw: true,
limit: 20
}).catch(function (err) {
console.log(err);
});
关于javascript - Sequelize MySQL从同一个表中选择(左连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812282/