简体   繁体   English

正确的MariaDB语法用于SQL替换语句

[英]Correct MariaDB Syntax for SQL Replace Statement

After trying to determine why my SQL statement was returning an error in the Node code I am refactoring to connect to MariaDB rather than via SQL Anywhere, I have narrowed it down to the REPLACE statement we use to compute how many records to process, and how many to skip. 在尝试确定为什么我的SQL语句为什么要重构节点代码而不是通过SQL Anywhere连接到MariaDB而不是通过SQL Anywhere后,我将其范围缩小到了REPLACE语句,我们使用该语句来计算要处理的记录数量以及如何处理许多要跳过。

My initial test SQL SELECT statement looks like this: 我的初始测试SQL SELECT语句如下所示:

SELECT customer_name FROM ar.customers

We then use a REPLACE statement to, as I say, determine how many records to process, and how many to skip. 然后,我们使用REPLACE语句来确定要处理的记录数和要跳过的记录数。 When we were using SQL Anywhere that looked like this: 当我们使用SQL Anywhere时,如下所示:

const sql = this.query.replace(/SELECT/i, `SELECT TOP ${recordsPerRun} START AT ${recordsProcessed + 1}`);

That syntax needs to change because MariaDB uses "LIMIT" instead of "TOP". 该语法需要更改,因为MariaDB使用“ LIMIT”而不是“ TOP”。 And from my understanding, the first parameter will be the number of records to skip, and the second one how many to return. 根据我的理解,第一个参数是要跳过的记录数,第二个参数是要返回的记录数。

So, in my case, it'd be something like: 因此,就我而言,它就像是这样:

LIMIT ${recordsProcessed}, ${recordsPerRun}

However, I can't quite get the full syntax right. 但是,我不太了解完整的语法。 How can I write this REPLACE statement in a way that will work with my initial test SQL SELECT statement from above? 如何以与上面的初始测试SQL SELECT语句兼容的方式编写此REPLACE语句? This seems tricky to do since in MariaDB LIMIT now goes at the end of the query, not at the beginning, like TOP did for MySQL. 这似乎很棘手,因为在MariaDB中, LIMIT现在位于查询的结尾,而不是开头,就像TOP对MySQL所做的那样。

LIMIT goes at the end, so there's nothing to replace, just concatenate it: LIMIT在末尾,所以没有什么可替换的,只需将其连接起来即可:

const sql = this.query + ` LIMIT ${recordsProcessed}, ${recordsPerRun}`;

or combine it into the template: 或将其合并到模板中:

const sql = `${this.query} LIMIT ${recordsProcessed}, ${recordsPerRun}`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM