使用 Sequelize 构建带有 WHERE 表别名的 JOIN 表 SQL 查询。

huangapple go评论49阅读模式
英文:

Building Sequelize JOIN table SQL queries with WHERE table alias

问题

假设我们可以使用Sequelize提供的`QueryInterface`,我们如何在`Where`子句中定义表别名?
        const queryInterface = this.sequelize.getQueryInterface();
        const queryGenerator = queryInterface.queryGenerator as SequelizeQueryGenerator;
        const whereObject = [
            {
                ['venueid']: {
                    [Op.lt]: 50
                }
            },
            {
                ['e.eventid']: { // 尝试在列上定义表别名
                    [Op.eq]: 100
                }
            }
        ];
        const sql = queryGenerator.selectQuery(
            [
                ['venue', 'v'],
                ['event', 'e']
            ],
            { where: whereObject },
            null
        );
实际输出:

SELECT * FROM "venue" AS "v", "event" AS "e" WHERE ("v"."venueid" < 50 AND "v"."e.eventid" = 100);


期望输出:

SELECT * FROM "venue" AS "v", "event" AS "e" WHERE ("v"."venueid" < 50 AND "e"."eventid" = 100);


我尝试使其工作的方法是在`whereObject`中直接提供别名
           {
                [&#39;e.eventid&#39;]: { // 尝试在列上定义表别名
                    [Op.eq]: 100
                }
            }
不使用模型进行查询的原因是我试图将其创建为一个无模型的查询服务。
英文:

Suppose we could use QueryInterface provided by sequelize, how can we define the Table Alias in the Where clause?

            const queryInterface = this.sequelize.getQueryInterface();
            const queryGenerator = queryInterface.queryGenerator as SequelizeQueryGenerator;
            const whereObject = [
                {
                    [&#39;venueid&#39;]: {
                        [Op.lt]: 50
                    }
                },
                {
                    [&#39;e.eventid&#39;]: { // Attempt to define Table Alias on column
                        [Op.eq]: 100
                    }
                }
            ];
            const sql = queryGenerator.selectQuery(
                [
                    [&#39;venue&#39;, &#39;v&#39;],
                    [&#39;event&#39;, &#39;e&#39;]
                ],
                { where: whereObject },
                null
            );

Actual output:

SELECT * FROM &quot;venue&quot; AS &quot;v&quot;, &quot;event&quot; AS &quot;e&quot; WHERE (&quot;v&quot;.&quot;venueid&quot; &lt; 50 AND &quot;v&quot;.&quot;e.eventid&quot; = 100);

Expected output:

SELECT * FROM &quot;venue&quot; AS &quot;v&quot;, &quot;event&quot; AS &quot;e&quot; WHERE (&quot;v&quot;.&quot;venueid&quot; &lt; 50 AND &quot;e&quot;.&quot;eventid&quot; = 100);

My attempts to make this work is by providing the Alias directly in the whereObject

               {
                    [&#39;e.eventid&#39;]: { // Attempt to define Table Alias on column
                        [Op.eq]: 100
                    }
                }

The reason for not using a Model for query is that I'm trying to make this a model-less query service.

答案1

得分: 0

If you use the where function, it seems to work.

如果您使用`where`函数它似乎可以工作

const whereObject = sequelize.and(
  sequelize.where(Sequelize.col('v.venue_id'), Op.lt, 50),
  sequelize.where(Sequelize.col('e.event_id'), 100)
)
英文:

If you use the where function, it seems to work.

const whereObject = sequelize.and(
  sequelize.where(Sequelize.col(&#39;v.venue_id&#39;), Op.lt, 50),
  sequelize.where(Sequelize.col(&#39;e.event_id&#39;), 100)
)

huangapple
  • 本文由 发表于 2023年2月14日 18:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定