为什么我的 MariaDB 查询不能在主机上运行但在本地运行?

Why is my MariaDB query not working on host but works locally?

这是我的代码,在本地运行良好,但在 Namecheap 主机上运行不正常:

//data connection pool
const pool = require('../models/database');
const bcrypt = require('bcrypt');

module.exports = async (req, res) => {
    try {
        const {user_name, password, email} = req.body;
        const hashedPassword = await bcrypt.hash(password, 10);
        console.log(hashedPassword);

        let sql = `INSERT INTO userdb (user_name, password, email)
        SELECT ?, ?, ? 
        WHERE NOT EXISTS (SELECT 1 FROM userdb WHERE email = ? OR user_name = ?);
        SHOW WARNINGS;`;

        let result = await pool.query(sql, [user_name, hashedPassword, email, email, user_name]);
        
        console.log(result);
        console.log("the id: ", result[0].insertId);
        res.status(200).send("Success Comrad!");

        } catch (error) {
        res.status(400).send(error.message);
    }
    
}//module.export end

这些是我的依赖项:

"dependencies": {
    "bcrypt": "^5.0.1",
    "dotenv": "^16.0.0",
    "express": "^4.17.2",
    "mariadb": "^2.5.5",
    "pug": "^3.0.2"
  }

这是我遇到的错误:

(conn=1698954, no: 1064, SQLState: 42000) 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 'WHERE NOT EXISTS (SELECT 1 FROM userdb WHERE email = 'dookie@gmail.com' OR us...' at line 3 sql: INSERT INTO userdb (user_name, password, email) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM userdb WHERE email = ? OR user_name = ?); SHOW WARNINGS; - parameters:['dookie','b$OQp5lovlqmMfKNQtuvBK9e7t4rX39nVW22XdhzDZqBnSgDKv6G0ky','d...]

我也有这套:multipleStatements: true 知道这是为什么吗?

您确定这在本地有效吗?也许忘记了一部分

您的 SELECT 需要 FROM 才能工作。

SELECT ?, ?, ? 
WHERE NOT EXISTS (/* removed */);

要回答您的第二个问题,multipleStatements: true 允许您一次传递多个查询。在这里您要求插入一些数据 以显示可能的警告。

我通过不同的方法解决了这个问题。我的主要目标是防止重复条目。

我首先通过这样做在 PHPMyAdmin 中设置了一个唯一约束:

ALTER TABLE userdb 
ADD CONSTRAINT UC_email UNIQUE (email)

然后简化了我的查询:

let sql = `INSERT INTO userdb (user_name, password, email)
        VALUES (?,?,?)`;