I have the following table
I have inserted Product B to it and it gives me an ID of 15
Then I have the definition table which is as follows.
I want to select the ProductDefinition rows where ProdID = 14 and replicate the same and insert it for ProdID = 15 like the following
How to achieve this using SQL code?
To select duplicate values, you need to create groups of rows with the same values and then select the groups with counts greater than one. You can achieve that by using GROUP BY and a HAVING clause.
UPDATE table SET col = new_value WHERE col = old_value AND other_col = some_other_value; UPDATE table SET col = new_value WHERE col = old_value OR other_col = some_other_value; As you can see, you can expand the WHERE clause as much as you'd like in order to filter down the rows for updating to what you need.
INSERT INTO ProductDefinition (ProdID, Definition, Desc) SELECT xxx, Definition, Desc FROM ProductDefinition WHERE ProdID = yyy
The xxx
is your new ProdID and the yyy
is your old one. This also assumes that DefID
is automagically populated on INSERT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With