如何在 AWS Lambda 函数中一次传递多个 MySQL 查询

How to pass multiple MySQL Queries in AWS Lambda Function at once

我在 Workbench 中创建了多个 MySQL 查询。此查询仅在 Workbench 中由一个触发器运行。我希望将此查询嵌入到我的 AWS Lambda 函数中,但无济于事。我怀疑是因为它同时查询了多个

这是我创建但失败的代码:

var mysql = require('mysql');
var config = require('./config.json');
var pool  = mysql.createPool({
       host     : config.dbhost,
       user     : config.dbuser,
       password : config.dbpassword,
       database : config.dbname
    });

exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  pool.getConnection(function(err, connection) {
      // Use the connection
      connection.query('DROP VIEW IF EXISTS view_table; \
                        CREATE VIEW view_table AS \
                            SELECT table3.field1, table1.field2 \
                            FROM table1 \
                            JOIN table2 ON table1.table2_id = table2.id \
                            JOIN table3 ON table1.table3_id = table3.id \
                            JOIN table4 ON item_var.table4_id = table4.id \
                            WHERE item_var.list_id = ? \
                            ORDER BY table1 ASC LIMIT 3 \
                        SELECT table4.field1, table2.field2, table2.field3, \
                            (SELECT field1 FROM view_table LIMIT 0, 1) AS field4, \
                            (SELECT field1 FROM view_table LIMIT 1, 1) AS field5, \
                            (SELECT field1 FROM view_table LIMIT 2, 1) AS field6 \
                        FROM table2 \
                        JOIN table4 ON table2.id = table4.id \
                        WHERE table2.id = ?', event['param1'], function (error, results, fields) {
      // And done with the connection.
      connection.release();
      // Handle error after the release.
      if (error) callback(error) ;
      else callback(null, results);
    });
  });
};

这是我收到的错误消息:

Response:
{
  "errorType": "Error",
  "errorMessage": "ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE VIEW view_table SELECT table3.field1,' at line 1",

我怀疑这是因为 Lambda 不能同时接受多个 MySQL 查询。

这里有一些选项,

  1. 使用 AWS SNS 通知;或
  2. 创建多个 lambda 并依次触发它们。

谁能指导我如何正确执行此操作?

这完全是因为 MySQL 不支持多查询,据我所知,您有 2 个查询。

您应该 而不是 根据您提供的内容并行尝试 运行,因为您不能保证顺序,为什么不作为两个单独的查询执行,例如如下。

var mysql = require('mysql');
var config = require('./config.json');
var pool  = mysql.createPool({
       host     : config.dbhost,
       user     : config.dbuser,
       password : config.dbpassword,
       database : config.dbname
    });

exports.handler = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  pool.getConnection(function(err, connection) {
      // Use the connection
      connection.query('DROP VIEW IF EXISTS view_table;', function (error, results, fields) {
      // Handle error after the release.
      if (error) callback(error) ;
    });
      connection.query('CREATE VIEW view_table AS \
                            SELECT table3.field1, table1.field2 \
                            FROM table1 \
                            JOIN table2 ON table1.table2_id = table2.id \
                            JOIN table3 ON table1.table3_id = table3.id \
                            JOIN table4 ON item_var.table4_id = table4.id \
                            WHERE item_var.list_id = ? \
                            ORDER BY table1 ASC LIMIT 3;', function (error, results, fields) {
      // Handle error after the release.
      if (error) callback(error) ;
    });
    
      connection.query('SELECT table4.field1, table2.field2, table2.field3, \
                            (SELECT field1 FROM view_table LIMIT 0, 1) AS field4, \
                            (SELECT field1 FROM view_table LIMIT 1, 1) AS field5, \
                            (SELECT field1 FROM view_table LIMIT 2, 1) AS field6 \
                        FROM table2 \
                        JOIN table4 ON table2.id = table4.id \
                        WHERE table2.id = ?', event['param1'], function (error, results, fields) {
      // And done with the connection.
      connection.release();
      // Handle error after the release.
      if (error) callback(error) ;
      else callback(null, results);
    });
  });
};

以上是让你删除视图,然后再执行创建视图。