1

I need to insert a field in a that references an id field in another table. The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)

INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'

There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.

table - codetype id bigserial primary key codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column

Table - typecategory ID big serial primary key categorydescription varchar

Jeff Vance
  • 11
  • 3

1 Answers1

0

If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.

Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL

maybe

UPDATE codetype c
SET    c.typecategoryid = t.id 
FROM   typecategory t
WHERE  c.codedescription = 'test' and t.categorydescription='Assessment Types'
Community
  • 1
  • 1
CLAbeel
  • 1,078
  • 14
  • 20