forked from andyatkinson/pg_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple_row_updates.sql
72 lines (62 loc) · 2.27 KB
/
multiple_row_updates.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
create database experiments;
-- \c experiments
create table list_items (
id serial primary key,
list_id integer,
position integer
);
-- \d list_items
-- Table "public.list_items"
-- +----------+---------+-----------+----------+----------------------------------------+
-- | Column | Type | Collation | Nullable | Default |
-- +----------+---------+-----------+----------+----------------------------------------+
-- | id | integer | | not null | nextval('list_items_id_seq'::regclass) |
-- | list_id | integer | | | |
-- | position | integer | | | |
-- +----------+---------+-----------+----------+----------------------------------------+
-- Indexes:
-- "list_items_pkey" PRIMARY KEY, btree (id)
-- add some rows
insert into list_items (list_id, position) SELECT 1, generate_series(1, 10, 1);
-- use update ... from syntax
-- https://stackoverflow.com/a/18799497
--
-- another example:
--
-- update users as u set -- postgres FTW
-- email = u2.email,
-- first_name = u2.first_name,
-- last_name = u2.last_name
-- from (values
-- (1, '[email protected]', 'Hollis', 'Connell'),
-- (2, '[email protected]', 'Robert', 'Duncan')
-- ) as u2(id, email, first_name, last_name)
-- where u2.id = u.id;
update list_items as li set
id = li2.id,
position = li2.position
from (values
(2, 1),
(1, 2)
) as li2(id, position)
where li2.id = li.id;
-- note that after the statement above, sorting the list items
-- by primary key id ascending order, we see the positions of 2 and 1
-- have swapped now, so row 1 is now in position "2",
-- and row 2 (pk: 2) is now in position "1"
-- [local]:5432 experiments# select * from list_items order by id asc;
-- +----+---------+----------+
-- | id | list_id | position |
-- +----+---------+----------+
-- | 1 | 1 | 2 |
-- | 2 | 1 | 1 |
-- | 3 | 1 | 3 |
-- | 4 | 1 | 4 |
-- | 5 | 1 | 5 |
-- | 6 | 1 | 6 |
-- | 7 | 1 | 7 |
-- | 8 | 1 | 8 |
-- | 9 | 1 | 9 |
-- | 10 | 1 | 10 |
-- +----+---------+----------+
-- (10 rows)