mysql - 在查询字符串中传递多个 id,并在节点 js 中的 mysql 中为它们获取数据
问题描述
我在 nodejs 中有一个 get API,它获取变量字符串作为查询字符串(由“,”分隔),如下所示
const mac = req.query.mac;
console.log(mac); // 00:11:22:33:FF:EE,11:11:22:33:FF:EE
var sql = mysql.format("SELECT * FROM user_email WHERE macId IN ?",[mac]);
connection.query(sql, function(err, row) ...{ ... .. }
但我收到错误
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''00:11:22:33:FF:EE,11:11:22:33:FF:EE'' at line 1",
sqlState: '42000',
index: 0,
sql: "SELECT * FROM user_email WHERE macId IN '00:11:22:33:FF:EE,11:11:22:33:FF:EE'"
}
有人请帮助!
解决方案
您的错误是将 mac 从字符串转换为数组
[mac]
不会将其转换为字符串。试试这个:
const mac = req.query.mac;
const macArr = mac.split(',');
var sql = mysql.format("SELECT * FROM user_email WHERE macId IN (?)", macArr);
connection.query(sql, function(err, row) ...{ ... .. }
推荐阅读
- android - 当 viewPager 位于主片段中时,如何从 Main Activity 更改 viewPager 的位置
- html - 将文本向上移动,同时与图片保持在同一行中
- java - src-resolve:无法将名称“tns:callResult”解析为(n)“类型定义”
- reactjs - 使用 react-testing 库提交 Formik 表单
- rust - 有没有办法在不使用 C ABI 的情况下在 ARM 上设置程序计数器?
- r - R:从数据框的行中堆叠值
- python - 创建新的 Dataframe 数据切片问题
- matlab - 如果循环中任何 Y 为零,Matlab 不会回归
- php - 如何在php url中添加id和title
- asp.net-core - 尝试更新记录后,我得到“值不能为空”