【问题标题】:Stored Procedure for node.jsnode.js 的存储过程
【发布时间】:2022-01-26 04:25:50
【问题描述】:
    User.createuser = (req, result) => {
      sql.query("INSERT INTO Users SET ?", req, (err, res) => {
        if (err) {
          console.log("error: ", err);
          result(err, null);
          return;
        }
    
        // if (await User.findOne({ where: { username: req.body.username } })) {
        //   throw 'Username "' + req.body.username + '" is already taken';
        // }
    
        console.log("created User: ", { id: res.insertId, ...req });
        result(null, { id: res.insertId, ...req });
      });
    };

api 创建路由(post 方法)的示例 json 对象

{   

    "username": "seve",
    "user_name": "sev123",
    "user_surname": "avcı",
    "email": "sevre@gmail.com",
    "user_type": "user",
    "password": "123456"
}

“插入用户集?”我在代码中使用的查询,我希望它作为一个存储过程,我希望它自己处理存储过程 id,这个代码的存储过程的等价物是什么?

【问题讨论】:

  • 您想按原样向查询提供显示的 JSON 并将其值插入到相应的表列中吗?如果是这样,那么发布 precise MySQL 版本。并澄清 JSON 结构是静态的(所有显示的属性始终存在),还是可能不同?

标签: mysql sql node.js mysql-workbench


【解决方案1】:
INSERT INTO users (username, user_name, user_surname, email, user_type, password)
SELECT x->>'$.username', x->>'$.user_name', x->>'$.user_surname', x->>'$.email', x->>'$.user_type', x->>'$.password'
FROM ( SELECT ? AS x ) jsondata

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f190bb4b9d4c3e4003bae631af901d72

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2020-12-26
    • 2017-01-28
    • 2016-07-24
    • 1970-01-01
    相关资源
    最近更新 更多