【问题标题】:Sequelize: Where condition with multiple AND, OR combinations on Joined tablesSequelize:连接表上具有多个 AND、OR 组合的条件
【发布时间】:2023-04-08 06:54:02
【问题描述】:

我的问题是对以下链接中提出的问题的扩展,并进行了一些更改,如下所述。

Where condition for joined table in Sequelize ORM

我想用 sequelize ORM 得到这样的查询:

SELECT "A".*,      
FROM "A" 
LEFT OUTER JOIN "B" ON "A"."bId" = "B"."id"
LEFT OUTER JOIN "C" ON "A"."cId" = "C"."id"
WHERE ("B"."userId" = '100' OR "C"."userId" = '100')
AND ("B"."orgId" = '101' OR "C"."orgId" = '102') 
AND ("B"."contentId" = 'content1' OR "C"."profileId" = 'profileId1');

我想要的是将连接表中的更多字段集合到多个 OR 条件中。就像上面的例子一样,与上面链接中提出的问题相比,在 where 条件中只添加了 userId,我想添加 orgId、contentId 等。

如上述链接中所述,最高票数 (74) 的答案可以正常工作,但仅适用于 userId 等单一字段集。当我想使用多个字段时,我遇到了问题。

根据上面链接中提供的答案,我在下面尝试了,但不幸的是,它只正确选择了一个 OR,其余的都被忽略了。

const whereCondition = {
    [Sequelize.Op.or]: [
        [
            { '$B.userId$': 100 },
            { '$C.userId$': 100 }
        ]
    ],
    [Sequelize.Op.or]: [
        [
            { '$B.orgId$': 101 },
            { '$C.orgId$': 102 }
        ]
    ],
    [Sequelize.Op.or]: [
        [
            { '$B.contentId$': 'content1' },
            { '$C.profileId$': 'profileId1' }
        ]
    ]
}

我怎样才能做到这一点?

【问题讨论】:

  • WHERE ("B"."userId" = '100' OR "C"."userId" = '100') 基本上 将你的LEFT JOINs 简化为一个普通的连接?

标签: javascript sql postgresql orm sequelize.js


【解决方案1】:
'use strick';


const Sequelize = require("sequelize");
const Op = Sequelize.Op;
const db = require("../models");

describe('db', () => {

    it('A B C', async () => {

        const whereCondition = {
            [Op.and]: [
                {
                    [Op.or]: [
                        { '$B.userId$': 100 },
                        { '$C.userId$': 100 }
                    ]
                },
                {
                    [Op.or]: [
                        { '$B.orgId$': 101 },
                        { '$C.orgId$': 102 }
                    ]
                },
                {
                    [Op.or]: [
                        { '$B.contentId$': 'content1' },
                        { '$C.profileId$': 'profileId1' }
                    ]
                }
            ]
        }
        let A = await db.A.findAll({
            include: ['B', 'C'],
            where: whereCondition
        });
        var aList = JSON.parse(JSON.stringify(A))
        console.log(aList)
        return
    })
})

更新:

[Op.or]: [
    [
       {'$B.contentId$' : 'content1'},
       {'$C.profileId$' : 'profileId1'}
    ]
 ] 

->

[Op.or]: [
    { '$B.contentId$': 'content1' },
    { '$C.profileId$': 'profileId1' }
]
[Op.and]: [ { ... }, { ... }, { ... } ]

使用“日志记录”进行调试

正在执行(默认):SELECT A.id, A.bId, A.cId, B.id AS B.id, @987654 987654336 @ AS B.orgIdBcontentId AS B.contentIdCid AS C.idCuserId AS C.userIdCprofileId AS C.profileId 987654350 orgId AS C.orgId A左外外连接B 987654356 A 987654359 id 987654360 id 987654360左外连接C AS C ON A.cId = C.id 哪里((B.userId = 100 或C10987654369@3) (B.orgId = 101 或 C.orgId = 102)和(B.contentId = 'content1' 或 C.profileId = 'profileId1'));

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
相关资源
最近更新 更多