我如何比较两列在Sequelize Postgres数据库使用node.js



在我的Postgres DB中,有两列,一列用于email,另一列用于存储roles,角色是customerengineer。在这里,一些邮件包含customer作为一个角色,一些邮件包含engineer作为一个角色。当我从DB检索数据时,我需要验证电子邮件是否与特定角色(客户/工程师)匹配。如何使用nodejs编写条件。

id | role     | email
---+----------+------------------------
1  | engineer | testemail01@gmail.com
---+----------+------------------------
2  | customer | testemail02@gmail.com
---+----------+------------------------
3  | engineer | testemail03@gmail.com
---------------------------------------

检索时,我需要比较testemail01@gmail.com是否为engineer

如果你只需要确保有一个指定的电子邮件和角色的记录,那么你可以使用count方法和简单的where条件,像这样:

const roleCount = await Model.count({
where: {
email: 'testemail01@gmail.com',
role: 'engineer'
}
})
const isEngineer = roleCount > 0;

最新更新