Skip to content
Advertisement

Save few values to the row in Postgres [closed]

Is it possible to save few values to the row. For example, I have 2 tables: For Participants:

CREATE TABLE participants (
  id PRIMARY KEY,
  uuid BIGINT UNIQUE
);

And for Voters:

CREATE TABLE voters (
  id PRIMARY KEY,
  uuid BIGINT UNIQUE,
  participants -> here I want to save all available uuid from participants table
);

I want to get array of participants uuid after executing SELECT participants FROM voters WHERE uuid='someuuid';

Advertisement

Answer

CREATE TABLE voters (
  id PRIMARY KEY,
  uuid BIGINT UNIQUE,
  participants INT[]
);

then later use unnest function to expand the array to row records

SELECT unnest(participants)
FROM voters 
WHERE uuid='someuuid'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement