i have a big problem i have been trying to solve for hours without luck.
I need to edit or insert lines into an existing table and getting the data from another table, but the second table has multiple lines with same productid.
The structure of these tables are different, one of them as an unique id (i think this is the right definition, but i am not sure), meaning that each line has its own unique id (i am sorry but i am not able to explain this with proper language, so i hope you understand) and that you will not find 2 lines with the same id. Table 2 has another structure and it has no unique id, it only has 2 fields, productid and categoryid, and i have multiple lines with the same productid
The table from which i need to get data has the following structure: id cid
The table where i need to insert or update data has the following structure: productid categoryid
The problem is that table 2 has multiple productids, meaning that i can have more than 1 line with the same productid (ex. productid 20 categoryid 3 and again another line with productid 20 and categoryid 4), and in my existing table 2 there are already many productid entries that have more than 1 line with the sameproduct id, because this is used to select multiple categories for a single product, and i would like not to lose existing information but also i would like to understand how to recreate this table 2 from scratch inserting new lines.
I need table 2 to have at least 1 line with table 1 id as productid and table1 cid as categoryid, if possible without deleting existing data of multiple categories in table 2.
So i have 2 solutions:
lose existing multiple categories information deleting table 2 and recreate it with some code similar to the code i used below inserting data from table 1, please check my code because i am a noob.
update table 2 and avoid losing data for multiple category but assure that i have at least 1 entry with the main categoryid for each productid.
What i need is to set the categoryid field of table 2 equal to the cid field of table 1 where productid of table 2 is equal to id of table 1
So basically something like:
INSERT INTO dz3_properties_product_category (productid, categoryid) SELECT
id, cid FROM dz3_properties_products;
These last query gives me duplicated primary key error, i do not understand what to do..
Maybe something like
update properties_products p join
properties_category l
on p.id = l.productid
set l.categoryid = p.cid;