mysql - sync-mysql node js中使用变量调用query

标签 mysql node.js

var mysql= require('sync-mysql');
var connection = new mysql({
    host: 'host-name',
    user: 'root',
    password: 'password',
    database: 'tables', //name of the database
    multipleStatements: true
});

var val="value"
const ans= connection.queueQuery("Select * from table Where col_1=val";);
console.log(ans());

尽管此类列存在一个元组,但查询返回的是一个空数组。 我如何在查询中使用局部变量?

最佳答案

你可以使用 ES6 Template literals:

const ans = connection.queueQuery(`Select * from table Where col_1 = ${val}`);

或者简单的连接:

const ans = connection.queueQuery('Select * from table Where col_1 = ' + val);

关于mysql - sync-mysql node js中使用变量调用query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51183840/

相关文章:

php - 在 join 的 where 子句中传递数组

mysql - 我的自定义嵌套 SQL 查询不起作用

c++ - 使用 NAN 在 Node.js 的 C++ 模块中未调用 SetAccessor 函数

node.js - Mongoose 仅​​使用 createdAt 时间戳

mysql - 我在使用 sql 创建触发器的代码中存在语法错误。我使用mysql版本14.14

特定数据库或用户的mysql一般查询日志?

mySQL数据库效率问题

javascript - 书籍及其图像未呈现在页面上

node.js - 尝试使用 Typescript 生成 Express 的生产版本

jquery - 使用 Cheerio.js 迭代表中的 TR