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

时间:2018-12-11 15:37:12

标签: mysql angular http insert-update node-mysql

我想对数据库中分成组的项目进行一定次数的乘法,但是当乘法时,我需要更改一个名为GroupNumber的字段的值。

为此,我正在循环(FOR)中发出两个HTTP请求。

第一个执行我需要的副本,第二个执行GroupNumber字段中的UPDATE。

image 1

您可以看到复制完成了3次,但是GroupNumber(组编号)的值假定被复制的产品组的值+1(以“ 2”表示),并且保持不变。我希望副本显示在下面。

Image 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); 
    })
})

0 个答案:

没有答案