Update Multiple Unique Rows in Single Query
See original GitHub issueI’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:
- Created 9 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You’re quite welcome! Just to clarify, you can basically stringify:
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.@58bits @bendrucker I’m trying to do something similar, but don’t understand how to build the values statement.
Here is what I have -
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