簡體   English   中英

Knex.js更新語句從多個表返回數據

[英]Knex.js update statement returning data from multiple tables

如何使用knex.js進行更新,其中returning數據來自多個表。 例如,如何使用knex.js編寫此更新查詢?

update notes 
set note = 'This is an updated1 note', timestamp = now() 
from(select
    id, 
    note, 
    user_id,
    customer_id
    from notes
    where id = '46' AND user_id = 7) a
    left join customers as b on a.customer_id = b.id
where notes.id = a.id
returning a.id, a.note, a.user_id, b.id;

當只使用一個表時,我可以寫它來返回數據:

knex('notes')
    .returning([
      'notes.id',
      'notes.note',
      'notes.user_id AS userId',
      'notes.customer_id AS customerId',
      'notes.product_id AS productId',
      'notes.timestamp',
    ])
    .where('notes.id', noteId)
    .update({
        timestamp: new Date(),
        note: req.body.note,
    });

當我嘗試介紹customers時,我的問題就開始了。

我也試過這個:

const [note] = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, a.note, a.user_id, b.id', ['This is an updated1 note', 46]
      );

但是它給我TypeError: (intermediate value) is not iterable

UPDATE

我現在有這個:

let note = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id, '
        + 'product_id, '
        + 'timestamp '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'LEFT JOIN products AS c ON a.product_id = c.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, a.note, a.user_id AS userId, a.customer_id AS customerId, a.product_id AS productId, a.timestamp, b.title AS customerName, b.code AS customerCode, c.title AS productName, c.code AS productCode', [req.body.note, noteId]
      );

      /*const [note] = await db.sequelize.knex('notes')
        .returning([
          'notes.id',
          'notes.note',
          'notes.user_id AS userId',
          'notes.customer_id AS customerId',
          'notes.product_id AS productId',
          'notes.timestamp',
          //'customers.title AS customerName',
          //'customers.code AS customerCode',
          //'products.title AS productName',
          //'products.code AS productCode',
        ])
        //.leftJoin('customers', 'notes.customer_id', 'customers.id')
        //.leftJoin('products', 'notes.product_id', 'products.id')
        .where('notes.id', noteId)
        .update({
          timestamp: new Date(),
          note: req.body.note,
        });*/

        console.log('PC NOTE', note.rows);
      [note] = note.rows;

      return res.json({ note });

它返回以下內容:

{
    "note": {
        "id": 46,
        "note": "This is an updated2 note",
        "userid": 7,
        "customerid": 111781,
        "productid": null,
        "timestamp": "2019-07-12T19:37:23.996Z",
        "customername": "PC CUSTOMER3",
        "customercode": "PC003",
        "productname": null,
        "productcode": null
    }
}

這幾乎是我所需要的,除了返回的鍵都是小寫字母。 我如何獲得駱駝式出現的useridcustomeridproductidcustomernamecustomercodeproductnameproductcode ,就像我在knex語句的RETURN部分中所說的那樣?

Knex對更新+聯接沒有任何特殊支持

https://github.com/tgriesser/knex/issues/2796

因此編寫原始查詢是您唯一的選擇。

這對我有用:

let note = await db.sequelize.knex.raw(
        'UPDATE notes '
        + 'SET note = ?, '
        + 'timestamp = NOW() '
        + 'FROM(SELECT id, '
        + 'note, '
        + 'user_id, '
        + 'customer_id, '
        + 'product_id, '
        + 'timestamp '
        + 'FROM notes '
        + 'WHERE id = ? '
        + 'AND user_id = 7) AS a '
        + 'LEFT JOIN customers AS b ON a.customer_id = b.id '
        + 'LEFT JOIN products AS c ON a.product_id = c.id '
        + 'WHERE notes.id = a.id '
        + 'RETURNING a.id, '
        + 'notes.note, '
        + 'notes.user_id AS "userId", '
        + 'notes.customer_id AS "customerId", '
        + 'notes.product_id AS "productId", '
        + 'notes.timestamp, '
        + 'b.title AS "customerName", '
        + 'b.code AS "customerCode", '
        + 'c.title AS "productName", '
        + 'c.code AS "productCode"', [newNote, noteId],
      );

      [note] = note.rows;

      return res.json({ note });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM