Node.JS 仅返回 MySQL“WHERE IN”子句的部分记录
Posted
技术标签:
【中文标题】Node.JS 仅返回 MySQL“WHERE IN”子句的部分记录【英文标题】:Node.JS is returning only a part of records for the MySQL "WHERE IN" clause 【发布时间】:2021-12-08 04:26:29 【问题描述】:我正在尝试执行以下GET
函数。
http://127.0.0.1:3000/sportfolioimages/getbyportfoliolist?portfolioid_list=69,70,71
我的代码在 Node.js 中,请查看下方。
const mysql = require('mysql2');
const errorCodes = require('source/error-codes');
const PropertiesReader = require('properties-reader');
const prop = PropertiesReader('properties.properties');
const con = mysql.createConnection(
host: prop.get('server.host'),
user: prop.get("server.username"),
password: prop.get("server.password"),
port: prop.get("server.port"),
database: prop.get("server.dbname")
);
exports.getSellerPortfolioItemImagesByPortfolioList = (event, context, callback) =>
const params = event.queryStringParameters;
if (!params || portfolioid_list == null)
context.callbackWaitsForEmptyEventLoop = false;
var response = errorCodes.missing_parameters;
callback(null, response)
else
const portfolioid_list = event.queryStringParameters.portfolioid_list;
context.callbackWaitsForEmptyEventLoop = false;
const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN (?)";
con.execute(sql, [portfolioid_list], function (err, result)
console.log(sql);
if (err)
console.log(err);
var response = errorCodes.internal_server_error;
callback(null, response);
else
var response =
"statusCode": 200,
"headers":
"Content-Type": "application/json"
,
"body": JSON.stringify(result),
"isBase64Encoded": false
;
callback(null, response)
);
;
我的代码总是返回我调用中值列表的第一个值。由于我的值列表是69,70,71
,它始终只返回匹配69
的记录,并且即使数据库中有记录,70
和71
也不会返回任何记录。
举个例子,下面是我执行上述GET
函数时得到的结果。
[
"idseller_portfolio_item_images": 22,
"idseller_portfolio_item": 69,
"image_url": "https://database.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
,
"idseller_portfolio_item_images": 23,
"idseller_portfolio_item": 69,
"image_url": "https://database.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
,
"idseller_portfolio_item_images": 31,
"idseller_portfolio_item": 69,
"image_url": "https://peresia3.s3.us-east-2.amazonaws.com/portfolio/IMG_20211020_114254-1634730049335.jpg"
,
"idseller_portfolio_item_images": 32,
"idseller_portfolio_item": 69,
"image_url": "https://database/portfolio/IMG_20211020_114254-1634730049335.jpg"
]
如果我直接在数据库中运行 MySQL 代码,我将能够毫无问题地获取完整的记录集。
为什么会这样,我该如何解决?
【问题讨论】:
【参考方案1】:根据this,您将需要这样的东西
const portfolioid_list = [69,70,71];
const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN (?,?,?)";
con.execute(sql, portfolioid_list, function (err, result) ...
这样您就可以动态地构建您的查询。 reference
const portfolioid_list = event.queryStringParameters.portfolioid_list.split(","); //converts to a list ["69", "70", "71"]
portfolioidValsPlaceHolders=Array(portfolioid_list.length).fill("?").join();
const sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN ("+portfolioidValsPlaceHolders+")";
con.execute(sql, portfolioid_list, function (err, result) ...
【讨论】:
好的,但是我应该如何知道列表的确切长度?似乎是一个错误。我现在将检查 git。 您可以将查询参数字符串解析为一个列表,然后使用列表的长度您可以构建“?”的编号在sql语句中需要。只是一个建议 你能帮我解决这个问题吗?我对 Node.js 知之甚少const portfolioid_list = event.queryStringParameters.portfolioid_list.split(","); //converts to a list ["69", "70", "71"]
let sql = "SELECT * FROM peresia.seller_portfolio_item_images WHERE idseller_portfolio_item IN (";
sql += "?,".repeat(portfolioid_list.length); //adding the "?"
sql = sql.substring(0, sql.length - 1); //remove the last unwanted ","
sql += ")"; //now the sql is complete
可能有更优雅的方式来做到这一点以上是关于Node.JS 仅返回 MySQL“WHERE IN”子句的部分记录的主要内容,如果未能解决你的问题,请参考以下文章
Node.js使用MySQL数据库中对RowDataPacket对象的使用