question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update Multiple Unique Rows in Single Query

See original GitHub issue

I’d like to do the following, via knex, against a PostgreSQL DB:

update test as t set
    column_a = c.column_a
from (values
    ('123', 1),
    ('345', 2)  
) as c(column_b, column_a) 
where c.column_b = t.column_b;

My values will come from an array, of indeterminate size. Apart from string building the query (in a loop), and using knex.raw, is there anything knex can do to lend a hand?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bendruckercommented, Nov 11, 2014

You’re quite welcome! Just to clarify, you can basically stringify:

knex
  .into('test t')
  .update('column_a', 'c.column_a')
  .where('c.column_b', 't.column_b')
  .toString();

Then build your values statement, then insert it before where, then call the whole string with knex.raw. Gets trickier if you want to use bindings, but it’s doable.

0reactions
alexc101commented, Nov 17, 2017

@58bits @bendrucker I’m trying to do something similar, but don’t understand how to build the values statement.

Here is what I have -

let string = knex
        .into('test t')
        .update('auth, c.auth')
        .from(knex.raw('values(1, 1, false), (2, 1, false)'))
        .where(knex.raw('c.id1 = t.id1 AND c.id2 = t.id2'))
        .toString()

        knex.raw(string)

Here is an sql fiddle of query I’m trying to recreate : http://sqlfiddle.com/#!17/62529/8

Hope you could help point me in the right direction?

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - UPDATE multiple rows with different values in one query ...
$sql = "SELECT COUNT(*) AS _num FROM test; INSERT INTO test(id) VALUES (1); SELECT COUNT(*) AS _num FROM test; "; $mysqli->multi_query($sql);. comparing result ......
Read more >
How to Update Multiple Records Using One Query in SQL ...
UPDATE TABLE_NAME SET COLUMN_VALUE = CASE COLUMN_NAME WHEN 'COLUMN_NAME1' THEN COLUMN_VALUE1 WHEN 'COLUMN_NAME2' THEN COLUMN_VALUE2 ELSE ...
Read more >
Update multiple rows (distinctive by primary key) with different ...
This tutorial demonstrates 3 ways to update multiple rows (distinctive by primary key) with different values in one query.
Read more >
Updating multiple rows with different values in one query
You will need to write very complicated conditions if you want to update more than two rows. In such a case you can...
Read more >
UPDATE multiple rows with different values in one query
Hello,. I wanted to know how to UPDATE multiple rows with different values and I just don't get it. For instance, three updates...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found