如何在现有 WHERE 条件下在 Sequelize js 中添加 Union?

how to add Union in Sequelize js on existing WHERE condition?

seqlelize中的where条件与下面的节点 哪里 A->

where: {
            [Op.or]: [{
                ownerid: {
                    [Op.in]: rows.map(row => row.ownerid),
                }
            }, {
                ownerid: {
                    [Op.notIn]: rows.map(row => row.ownerid),
                },
             shop: 1
            }]
        }

会生成

select *
    from users 
    WHERE (ownerId in ('12', '166') )  or    
        (ownerId not IN ('12','166')  and shop = 1 )

在 postgresql 中

我想将另一个条件添加到 where(UNION 条件)到这个 where CONDITION : LIKE BELOW GIVEN

哪里 B->

where: {
        ownerid: {
                    [Op.eq]: 13 
                }
             shop: 1
        }

我要生成

(
    select *
    from users
    where ownerId = 13 and shop = 1
)
union
(
    select *
    from users 
    WHERE (ownerId in ('12', '166') and `delete` is null)  or    
        (ownerId not IN ('12','166')  and shop = 1 and `delete` is null)
)

期望的结果

where: {
            [Op.or]: [{
                ownerid: {
                    [Op.in]: rows.map(row => row.ownerid),
                }
            }, {
                ownerid: {
                    [Op.notIn]: rows.map(row => row.ownerid),
                },
             shop: 1
            }]
        } 

PLUS

where: { ownerid: { [Op.eq]: 13 } shop: 1 }

创建

**

(
        select *
        from users
        where ownerId = 13 and shop = 1
    )
    union
    (
        select *
        from users 
        WHERE (ownerId in ('12', '166') and `delete` is null)  or    
            (ownerId not IN ('12','166')  and shop = 1 and `delete` is null)
    )

**

PS : ID 13是动态的,所有者id 13可以更改为其他号码。

如果您想要连接来自相同 table 但条件不同的记录,则无需使用 union。您可以通过 OR.
加入他们 SQL 可能如下所示:

select *
from users 
WHERE (ownerId in ('12', '166') and `delete` is null)  or    
  (ownerId not IN ('12','166')  and shop = 1 and `delete` is null) or
  (ownerId = 13 and shop = 1)

Sequelize 查询条件可能如下所示:

where: {
  [Op.or]: [{
    ownerid: {
      [Op.in]: rows.map(row => row.ownerid),
    }
  }, {
    ownerid: {
      [Op.notIn]: rows.map(row => row.ownerid),
    },
    shop: 1
  }, {
    ownerid: 13,
    shop: 1
  }]
}