Knex RAW MySQL 查询添加新行

Knex RAW MySQL query adding new lines

我是 MySQL 的新手,以前使用 PSQL 的方式与我使用 MySQL 的方式相同,但下面的代码有误。

return await db
        .raw(
            `INSERT INTO users(firstName, 
            lastName, 
            email, 
            password, 
            type,
            addressLine1, 
            addressLine2, 
            addressLine3, 
            mobileNumber,
            townCity, 
            postcode)
            VALUES (
            '${firstName}',
            '${lastName}',
            '${email}',
            '${hashedPassword}', 
            '${mobileNumber}',
            ${type}, 
            '${addressLine1}', 
            '${addressLine2}', 
            '${addressLine3}', 
            '${townCity}', 
            '${postcode}'); 
            SELECT * from users WHERE id = LAST_INSERT_ID()`,
        )
        .then(() => ...)

返回以下错误。

  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlState: '42000',
  sqlMessage: "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 'SELECT * from users WHERE id = LAST_INSERT_ID()' at line 24",
  sql: 'INSERT INTO users(firstName, \n' +
    '            lastName, \n' +
    '            email, \n' +
    '            password, \n' +
    '            type,\n' +
    '            addressLine1, \n' +
    '            addressLine2, \n' +
    '            addressLine3, \n' +
    '            mobileNumber,\n' +
    '            townCity, \n' +
    '            postcode)\n' +
    '            VALUES (\n' +
    "            'Jane',\n" +
    "            'Doe',\n" +
    "            'janedoe@email.com',\n" +
    "            'password', \n" +
    "            '07123456789',\n" +
    '            100, \n' +
    "            '1', \n" +
    "            'Test road', \n" +
    "            'Westminster', \n" +
    "            'London', \n" +
    "            'L1 1TG'); \n" +
    '            SELECT * from users WHERE id = LAST_INSERT_ID()'
}

我怀疑它与模板文字有关,但不确定解决此问题的最佳方法,它在 PSQL 中运行良好(不包括最后一行)。有没有更好的使用raw的方法SQL,我对所有高级ORM命令都不感兴趣

看来您不能在一个查询中执行太多操作。插入工作正常后将 SELECT * from users WHERE id = LAST_INSERT_ID() 提取到它自己的查询中

您需要启用 multipleStatements

const mysql = require('mysql')

connection = mysql.createConnection({
    host: 'localhost',
    user: 'youruser',
    password: 'yourpassword',
    database: 'yourdb',
    multipleStatements: true
})
connection.connect()

connection.query('INSERT INTO users(firstName, 
            lastName, 
            email, 
            password, 
            type,
            addressLine1, 
            addressLine2, 
            addressLine3, 
            mobileNumber,
            townCity, 
            postcode)
            VALUES (
            '${firstName}',
            '${lastName}',
            '${email}',
            '${hashedPassword}', 
            '${mobileNumber}',
            ${type}, 
            '${addressLine1}', 
            '${addressLine2}', 
            '${addressLine3}', 
            '${townCity}', 
            '${postcode}'); 
            SELECT * from users WHERE id = LAST_INSERT_ID();', function(err, results) {
    if (err) {
        throw err
    }

    console.log(results[0]])
})

connection.end()

尝试单独执行第一个查询。如果它没有执行,请检查您的 table 结构。似乎结构缺少主键设置或迁移时的某些内容