如何在数据库中插入记录,然后更新一些字段?

yptwkmov  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(213)

我想对数据库中被分成若干组的项进行一定次数的乘法,但在进行乘法时,我需要更改名为groupnumber的字段的值。
为此,我在一个循环(for)中发出两个http请求。
第一个复制了我需要的内容,第二个更新了groupnumber字段。
图1
如您所见,复制操作进行了3次,但groupnumber(group#)的值假定复制的产品组的值为+1(在“2”情况下),并且保持不变。我想把副本放在下面。
图2
按照规范进行审核:

products.component.ts

copyProductsOfGroup() {
    const quoteID = + this.activatedRoute.snapshot.paramMap.get('id');
    const groupNumber = this.copyGroupProductsForm.get('copyGroup').value
    const multiplier = this.copyGroupProductsForm.get('multiplier').value

    for (let i = 0; i < multiplier; i++) {
      this.productService.copyGroupProduct(quoteID, groupNumber)
        .subscribe(
          (cloneProductsInfos) => { 
            this.cloneProductsInfos = cloneProductsInfos
            console.log(this.cloneProductsInfos)
          },
          (err) => { 
            console.log(err) 
          },
          () => { 
            this.productService.updateCopyGroupProduct(this.groupNumber, this.cloneProductsInfos.insertId, this.cloneProductsInfos.affectedRows)
              .subscribe(
                (data) => {
                  console.log(data)
                },
                (err) => { 
                  console.log(err) 
                },
                () => {
                  this.getQuotesProducts() 
                  this.filterGroupNumber()
                  this.modalService.dismissAll()
                }
              )
          }
        )
    }
  }

服务:

product.service.ts

  copyGroupProduct(quoteID: number, groupNumber: number): Observable<CloneProductsModel> {
    const url = `http://localhost:9095/copy-group/${quoteID}/${groupNumber}`
    return this.http.get<CloneProductsModel>(url)
  }

  updateCopyGroupProduct(newGroupNumber: number, insertID: number, affectedRows: number): Observable<CloneProductsModel> {
    const url = `http://localhost:9095/copy-group-update/${newGroupNumber}/${insertID}/${affectedRows}`
    return this.http.get<CloneProductsModel>(url)
  }

nodejs和mysql:

// Copy productGroup
app.get('/copy-group/:quoteID/:groupNumber', function (req, res) {

    let quote_id = req.params.quoteID;
    let groupNumber = req.params.groupNumber;

    mydb.query(`
        INSERT INTO products (
            ProductName,
            ProductElement,
            ProductAttribute,
            ProductAttributeValue,
            Quantity,
            ProductNotes,
            FK_ID_QUOTE,
            ID_POF,
            ID_POE
        ) 
        (
            SELECT 
                ProductName,
                ProductElement,
                ProductAttribute,
                ProductAttributeValue,
                Quantity,
                ProductNotes,
                FK_ID_QUOTE,
                ID_POF,
                ID_POE
            FROM products AS P
            WHERE P.FK_ID_QUOTE = ${quote_id} AND P.GroupNumber = ${groupNumber}
        )`,
        function (error, results, fields) {
            if (error) throw error;
            console.log(results)
            return res.send(results);
        });
});

app.get('/copy-group-update/:newGroupNumber/:insertID/:affectedRows', function (req, res) {
    let newGroupNumber = req.params.newGroupNumber;
    let insertID = req.params.insertID;
    let affectedRows = req.params.affectedRows;

    console.log(newGroupNumber)
    console.log(insertID)
    console.log(affectedRows)

    mydb.query(`
        UPDATE products AS P
        SET P.GroupNumber = ${newGroupNumber}
        WHERE P.ID BETWEEN ${insertID} AND (${insertID} + ${affectedRows} - 1)
    `,
    function (error, results, fields) {
        if (error) throw error;
        console.log(results)
        return res.send(results); 
    })
})

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题