[英]How to do Inner Join in SEQUELIZE?
我有两张桌子:
表 1 :
ID | 姓名 | 电子邮件 |
---|---|---|
第一的 | 排 | 排 |
第二 | 排 | 排 |
表 2 :
ID | 数字 | 地位 |
---|---|---|
第一的 | 排 | 排 |
第二 | 排 | 排 |
我已经为每个模型提供了一个 JS 文件。
这是针对表 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;
这是针对表 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;
我想通过 Sequelize 使用 Inner Join。 我看到我可以使用原始 SQL 脚本,但由于我使用的是 Sequelize,所以我想充分利用它并利用它而不是使用原始 SQL。
我如何为此进行内部连接?
我要使用的 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';
同样,我正在寻找一种在 Sequelize 中执行此操作的方法,而不仅仅是使用原始 SQL。
试试看
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.