mysql - NodeJS 和 MySQL : Error in SQL syntax leading to UnhandledPromiseRejectionWarning

标签 mysql node.js

我正在使用 NodeJS 和 MySQL 以及异步函数查询数据库。我的异步函数中有以下查询:

async function get_data(sample) {

let pool, con;
try {
    pool = await mysql.createPool(config.databaseOptions);
    con = await pool.getConnection();
} catch (err) {
    console.error('Could not connect to database');
    return false;
}

var qry = "SELECT " +
"   t1.sample_id, t1.v_id, t1.s_type as type, t1.sf, " +
"   t1.ef, CONCAT(t1.chr_bkpt1, ':', t1.pos_bkpt1) as b1, " +
"   CONCAT(t1.chr_bkpt2, ':', t1.pos_bkpt2) as b2, " +
"   t1.sge, t1.ege, t1.pscore as som_score, " +
"   t1.wgs, t1.frame, t1.platform, t1.rconf, t1.report, " +
"   t1.target, t1.pclass, t1.evidence, t2.summary, t3.comments " +
"   FROM " +
"   ( " +
"       SELECT s.* " +
"       FROM sample_som s " +
"       WHERE sample_id=? " +
"   ) as t1 " +
"   LEFT JOIN " +
"   ( " +
"       SELECT v_id, c.sample_id, " +
"       CONCAT(c.sample_id,'(',e.disease_type,'): ',c.cur_summary) as summary " +
"       FROM samples e " +
"       LEFT JOIN sample_som c " +
"       ON e.sample_id=c.sample_id " +
"       WHERE c.cur_summary is not null " +
"       AND c.sample_id=? " +
"   ) as t2 " +
"   ON t1.v_id=t2.v_id " +
"   LEFT JOIN ( " +
"       SELECT v_id, c.sample_id, " +
"       CONCAT(c.sample_id,'(',e.disease_type,'): ',c.comments) as comments " +
"       FROM samples e " +
"       LEFT JOIN sample_som c " +
"       ON e.sample_id=c.sample_id " +
"       WHERE c.comments is not null " +
"       AND c.sample_id=? " +
"   ) as t3 " +
"   ON t1.v_id=t3.v_id " +
"   ORDER BY t1.v_id";

const [rows,fields] = await con.query(qry, sample);
await con.release();
return rows;
}

但是,当我运行服务器时出现以下错误:

UnhandledPromiseRejectionWarning: 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 '?)    as t2    ON t1.v_id=t2.v_id    LEFT JOIN (        SELECT v_id' at line 1

我不确定为什么会收到此错误以及如何解决此问题。任何见解都值得赞赏。

最佳答案

tl;dr 你的 sample数组不够长。

在这种情况下,仔细阅读错误消息可以确定您的问题。 MySQL 无法解析您的查询。它不理解的第一个字符是你的第二个 ?占位符。

你有三个 ?查询中的占位符,并且您提供了 sample调用 con.query(qry, sample) 时的参数.该参数必须是一个数组,其元素数与您的占位符数相同。

您还没有提供 catch处理程序围绕您的查询操作,因此当您的查询失败时, Node 会抛出 UnhandledPromiseRejectionWarning。 专业提示:始终捕获并处理来自 DBMS 的错误。

您可以使用多行字符串常量来保存长查询。例如

const qry = `
SELECT a,b
  FROM c
 WHERE id = ?
 ORDER BY b;`;

专业提示:这些比您在示例中使用的样式更易于阅读和编辑,尤其是对于像您这样的复杂查询。

关于mysql - NodeJS 和 MySQL : Error in SQL syntax leading to UnhandledPromiseRejectionWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58800957/

相关文章:

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - 如何在使用 NodeJS 和 Express 时创建 MySQL 连接池?

javascript - Expressjs - 如何为每个请求获取 '/' 路由?

mysql - 如何匹配两个表之间varchar字段中的字符串

Mysql多条件一列子查询

javascript - 我如何使用 CodeIgniter 和 jquery 调用 SQL 查询并将其加载到 DATATABLE 插件?

php - 如何在 phpMyAdmin -sql 中为两个表的主键使用唯一值? (在 PHP 中使用)

javascript - 使用 Node.js 从其他文件调用方法

javascript - vscode 不跳转到定义

javascript - 错误 : Cannot resolve module 'babel-loader'