No real way for me to succinctly summarize this one. I have two database tables (PostgreSQL 9.x)
The first one contains Products, for example. Each product has a name and a unique ID.
The second table contains sales records and has the name of each product sold but not the ID. I want to update this table with the ‘product_id’ of the corresponding unique entry in the Products table. However, sometimes, product names show up in the Sales table that do not exist in the Products table. When I find one of these, I want to add a product record to the Products table and then update the sales record(s) for that product name with the new product_id foreign key.
I have some SQL that does the first part: updates the sales record with the product_id that corresponds with the unique name in the Products table. I just need to know how to first sweep the sales table for unknown product names and create them in the Products table.
Something like this: