[英]How to do INNER JOIN on two tables without common columns using Sequelize in Nodejs?
I have two tables.我有两张桌子。
Campaigns活动
id ![]() |
cause![]() |
GoalType![]() |
Target![]() |
---|---|---|---|
1 ![]() |
5 ![]() |
0 ![]() |
5000 ![]() |
2 ![]() |
4 ![]() |
1 ![]() |
34500 ![]() |
3 ![]() |
5 ![]() |
1 ![]() |
50000 ![]() |
4 ![]() |
1 ![]() |
0 ![]() |
9000 ![]() |
5 ![]() |
8 ![]() |
1 ![]() |
35000 ![]() |
6 ![]() |
3 ![]() |
1 ![]() |
60000 ![]() |
Model defined as: Model 定义为:
module.exports = (sequelize, Sequelize) => {
const Campaigns = sequelize.define('Campaigns', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
cause: Sequelize.INTEGER,
goalType: Sequelize.INTEGER,
target: Sequelize.FLOAT,
}, {
freezeTableName: true,
});
return Campaigns
}
Campaign_Tasks Campaign_Tasks
id ![]() |
uuid ![]() |
action![]() |
Status![]() |
userId![]() |
---|---|---|---|---|
1 ![]() |
001-0000004 ![]() |
2 ![]() |
0 ![]() |
50 ![]() |
2 ![]() |
001-0000004 ![]() |
1 ![]() |
4 ![]() |
60 ![]() |
3 ![]() |
001-0000003 ![]() |
2 ![]() |
7 ![]() |
50 ![]() |
4 ![]() |
001-0000007 ![]() |
0 ![]() |
10 ![]() |
40 ![]() |
5 ![]() |
001-0000010 ![]() |
1 ![]() |
3 ![]() |
30 ![]() |
6 ![]() |
001-0000003 ![]() |
0 ![]() |
5 ![]() |
50 ![]() |
Model defined as: Model 定义为:
module.exports = (sequelize, Sequelize) => {
const Campaign_Tasks = sequelize.define('Campaign_Tasks', {
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
userId: Sequelize.INTEGER,
uuid: Sequelize.STRING,
action: Sequelize.INTEGER,
status: Sequelize.INTEGER,
}, {
freezeTableName: true,
});
return Campaign_Tasks
}
Each UUID in the tasks table corresponds to the id in the campaign table(Each UUID is generated indirectly from id, and this process is reversible). tasks 表中的每个 UUID 对应于campaign 表中的 id(每个 UUID 都是由 id 间接生成的,这个过程是可逆的)。 But as can be seen, there are no common columns in both tables.
但可以看出,两个表中都没有公共列。 I need to find Inner Join of these two tables based on id from campaign table and uuid from campaign_Tasks table (where uuid is not a PK of the campaign_Tasks table)
我需要根据活动表中的 id 和活动表中的 uuid 找到这两个表的内连接(其中 uuid 不是活动表的 PK)
By following this tutorial: https://lorenstewart.me/2016/09/12/sequelize-table-associations-joins/ I created a db object.通过遵循本教程: https://lorenstewart.me/2016/09/12/sequelize-table-associations-joins/我创建了一个数据库 object。 This was necessary because I couldn't define the associations in either of the model files as I kept getting the error mentioned here: hasMany called with something that's not an instance of Sequelize.Model
这是必要的,因为我无法在 model 文件中定义关联,因为我不断收到此处提到的错误: hasMany 调用的东西不是 Sequelize.Model 的实例
const db = require('./models/index')
const db_ = {};
db_.Sequelize = db.Sequelize;
db_.sequelize = db.sequelize;
//Models/tables
db_.campaigns = require('./models/campaigns')(db.sequelize, db.Sequelize);
db_.campaign_Tasks = require('./models/campaignTasks')(db.sequelize, db.Sequelize);
//Relations
db_.campaign_Tasks.belongsTo(db_.campaigns);
db_.campaigns.hasMany(db_.campaign_Tasks);
module.exports = db_;
How can I do Inner Join of both of these tables.我怎样才能对这两个表进行内部联接。 I am looking for something like this:
我正在寻找这样的东西:
const uuidToId = require('../../helpers/uuidToId')
let rows = await db_.campaign_Tasks.findAll({
include: [{
model: db_.campaigns,
required: true,
where: {
id: uuidToId.uuidToId(/*<-uuid from campaign_Tasks rows->*/)
}
}]
})
uuidToId converts UUID back to id. uuidToId 将 UUID 转换回 id。 Thanks in advance.
提前致谢。
There are three solutions that come to my mind:我想到了三个解决方案:
I've never used a cross-join because I try to keep my queries as simple as possible since it allows me to cache intermediate results.我从来没有使用过交叉连接,因为我试图让我的查询尽可能简单,因为它允许我缓存中间结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.