[英]How to do Inner Join in SEQUELIZE?
I have two tables:我有两张桌子:
Table 1 :表 1 :
ID ID | Name姓名 | Email电子邮件 |
---|---|---|
First第一的 | row排 | row排 |
Second第二 | row排 | row排 |
Table 2 :表 2 :
ID ID | Number数字 | Status地位 |
---|---|---|
First第一的 | row排 | row排 |
Second第二 | row排 | row排 |
I already have a model JS file for each.我已经为每个模型提供了一个 JS 文件。
This is for Table 1:这是针对表 1:
import Sequelize from "sequelize";
import db from './index.js';
const table1 = db.define("table_1", {
id : {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
name: {
type: Sequelize.STRING(100),
},
email: {
type: Sequelize.STRING(100),
}
}, {
schema: 'example',
tableName: 'table1'
});
export default table1;
This is for Table 2:这是针对表 2:
import Sequelize from "sequelize";
import db from './index.js';
const table2 = db.define("table_2", {
id : {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
number: {
type: Sequelize.STRING(100),
},
status: {
type: Sequelize.STRING(50),
}
}, {
schema: 'example',
tableName: 'table2'
});
export default table2;
I want to use Inner Join using Sequelize.我想通过 Sequelize 使用 Inner Join。 I saw I could use raw SQL script, but since I'm using Sequelize, I wanted to make good use of that and just utilize it instead of using the raw SQL.我看到我可以使用原始 SQL 脚本,但由于我使用的是 Sequelize,所以我想充分利用它并利用它而不是使用原始 SQL。
How do I do Inner Join for this one?我如何为此进行内部连接?
The SQL Script I'm going for is something like this:我要使用的 SQL 脚本是这样的:
SELECT
t1.id,
t1.name,
t2.email,
t2.number,
t2.status
FROM
table2 t2
INNER JOIN table1 t1
on t1.id = t2.id
WHERE
t2.email = 'test.test@test.com';
Again I'm looking for a way to do this in Sequelize instead of just using raw SQL.同样,我正在寻找一种在 Sequelize 中执行此操作的方法,而不仅仅是使用原始 SQL。
Try it试试看
Product.hasOne(ProductCategory, { as: 'category', foreignKey: 'productId' });
const products = await Product.findAll({
include: [
{
model: model.ProductCategory,
as: 'category',
required: true
}
]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.