简体   繁体   English

如何在Nodejs中使用Sequelize在没有公共列的两个表上进行INNER JOIN?

[英]How to do INNER JOIN on two tables without common columns using Sequelize in Nodejs?

I have two tables.我有两张桌子。

Campaigns活动

id ID cause原因 GoalType目标类型 Target目标
1 1 5 5 0 0 5000 5000
2 2 4 4 1 1 34500 34500
3 3 5 5 1 1 50000 50000
4 4 1 1 0 0 9000 9000
5 5 8 8 1 1 35000 35000
6 6 3 3 1 1 60000 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 ID uuid uuid action行动 Status地位 userId用户身份
1 1 001-0000004 001-0000004 2 2 0 0 50 50
2 2 001-0000004 001-0000004 1 1 4 4 60 60
3 3 001-0000003 001-0000003 2 2 7 7 50 50
4 4 001-0000007 001-0000007 0 0 10 10 40 40
5 5 001-0000010 001-0000010 1 1 3 3 30 30
6 6 001-0000003 001-0000003 0 0 5 5 50 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:我想到了三个解决方案:

  1. Run two separate queries where you first get all Campaign ID's, generate an appropriate uuid, use it in the second query, and merge the result in your code (my preferred way);运行两个单独的查询,首先获取所有活动 ID,生成适当的 uuid,在第二个查询中使用它,然后将结果合并到代码中(我的首选方式);
  2. Create a view in your database that will contain processed results and select against the view (note that if you're using computed columns, you won't be able to index them as MySQL doesn't support that as of right now);在您的数据库中创建一个视图,该视图将包含已处理的结果和 select 针对该视图(请注意,如果您使用计算列,您将无法索引它们,因为 MySQL 目前不支持该功能);
  3. Use something called a cross-join.使用称为交叉连接的东西。 It's not a join per se, but it will compute a cartesian product of your two tables.它本身不是连接,但它会计算两个表的笛卡尔积。 https://www.mysqltutorial.org/mysql-cross-join/ https://www.mysqltutorial.org/mysql-cross-join/

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.

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