SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE IF NOT EXISTS songs ( song_id integer NOT NULL, counter integer NOT NULL, PRIMARY KEY (song_id) ); INSERT INTO songs (song_id, counter ) VALUES (1, 0); INSERT INTO songs (song_id, counter ) VALUES (2, 0); INSERT INTO songs (song_id, counter ) VALUES (3, 0); INSERT INTO songs (song_id, counter ) VALUES (4, 0); INSERT INTO songs (song_id, counter ) VALUES (5, 0); INSERT INTO songs (song_id, counter ) VALUES (6, 0); INSERT INTO songs (song_id, counter ) VALUES (7, 0); INSERT INTO songs (song_id, counter ) VALUES (8, 0); CREATE TABLE IF NOT EXISTS counter ( song_id integer NOT NULL, counter integer NOT NULL, PRIMARY KEY (song_id) ); CREATE type counter2 AS (song_id int, counter int); select json_array_elements('[{"song_id": 1, "counter": 3}, {"song_id": 4, "counter": 4}]') as rndm; select * from songs, json_populate_recordset(null::counter, '[{"song_id": 1, "counter": 3}, {"song_id": 4, "counter": 4}]') as counters where songs.song_id = counters.song_id; with rndm as ( select song_id, counter from json_populate_recordset(null::counter2, '[{"song_id": 1, "counter": 3}, {"song_id": 4, "counter": 4}]')) update songs INNER JOIN rndm ON songs.song_id = rndm.song_id set songs.counter = songs.counter + rndm.counter; with counters as ( select song_id, counter from json_populate_recordset(null::counter2, '[{"song_id": 1, "counter": 3}, {"song_id": 4, "counter": 4}]')) update songs set counter = songs.counter + counters.counter FROM counters where songs.song_id = counters.song_id; select * from songs; select song_id, counter from json_populate_recordset(null::counter2, '[{"song_id": 1, "counter": 3}, {"song_id": 4, "counter": 4}]');
Stuck with a problem? Got Error? Ask ChatGPT!
Copy Clear